unix cron tab features

21

Click here to load reader

Upload: smruti2012

Post on 24-Oct-2014

84 views

Category:

Documents


1 download

DESCRIPTION

Unix Cron tab Features

TRANSCRIPT

Page 1: Unix Cron Tab Features

In this article, let us review 15 awesome examples of crontab job scheduling.

Linux Crontab FormatMIN HOUR DOM MON DOW CMD

Table: Crontab Fields and Allowed Ranges (Linux Crontab Syntax)Field Description Allowed Value

MIN Minute field 0 to 59

HOUR Hour field 0 to 23

DOMDay of Month

1-31

MON Month field 1-12

DOW Day Of Week 0-6

CMD Command Any command to be executed.

1. Scheduling a Job For a Specific Time Every Day

The basic usage of cron is to execute a job in a specific time as shown below. This will execute the Full backup shell script (full-backup) on 10th June 08:30 AM.

Please note that the time field uses 24 hours format. So, for 8 AM use 8, and for 8 PM use 20.

30 08 10 06 * /home/ramesh/full-backup

30 – 30th Minute 08 – 08 AM 10 – 10th Day 06 – 6th Month (June) * – Every day of the week

2. Schedule a Job For More Than One Instance (e.g. Twice a Day)

The following script take a incremental backup twice a day every day.

This example executes the specified incremental backup shell script (incremental-backup) at 11:00 and 16:00 on every day. The comma separated value in a field specifies that the command needs to be executed in all the mentioned time.

00 11,16 * * * /home/ramesh/bin/incremental-backup

00 – 0th Minute (Top of the hour) 11,16 – 11 AM and 4 PM * – Every day * – Every month * – Every day of the week

Page 2: Unix Cron Tab Features

3. Schedule a Job for Specific Range of Time (e.g. Only on Weekdays)

If you wanted a job to be scheduled for every hour with in a specific range of time then use the following.

Cron Job everyday during working hours

This example checks the status of the database everyday (including weekends) during the working hours 9 a.m – 6 p.m

00 09-18 * * * /home/ramesh/bin/check-db-status

00 – 0th Minute (Top of the hour) 09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm * – Every day * – Every month * – Every day of the week

Cron Job every weekday during working hours

This example checks the status of the database every weekday (i.e excluding Sat and Sun) during the working hours 9 a.m – 6 p.m.

00 09-18 * * 1-5 /home/ramesh/bin/check-db-status

00 – 0th Minute (Top of the hour) 09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm * – Every day * – Every month 1-5 -Mon, Tue, Wed, Thu and Fri (Every Weekday)

4. How to View Crontab Entries?

View Current Logged-In User’s Crontab entries

To view your crontab entries type crontab -l from your unix account as shown below.

ramesh@dev-db$ crontab -l@yearly /home/ramesh/annual-maintenance*/10 * * * * /home/ramesh/check-disk-space

[Note: This displays crontab of the current logged in user]

View Root Crontab entries

Login as root user (su – root) and do crontab -l as shown below.

root@dev-db# crontab -lno crontab for root

Crontab HowTo: View Other Linux User’s Crontabs entries

To view crontab entries of other Linux users, login to root and use -u {username} -l as shown below.

Page 3: Unix Cron Tab Features

root@dev-db# crontab -u sathiya -l@monthly /home/sathiya/monthly-backup00 09-18 * * * /home/sathiya/check-db-status

5. How to Edit Crontab Entries?

Edit Current Logged-In User’s Crontab entries

To edit a crontab entries, use crontab -e as shown below. By default this will edit the current logged-in users crontab.

ramesh@dev-db$ crontab -e@yearly /home/ramesh/centos/bin/annual-maintenance*/10 * * * * /home/ramesh/debian/bin/check-disk-space~"/tmp/crontab.XXXXyjWkHw" 2L, 83C

[Note: This will open the crontab file in Vim editor for editing.Please note cron created a temporary /tmp/crontab.XX... ]

When you save the above temporary file with :wq, it will save the crontab and display the following message indicating the crontab is successfully modified.

~"crontab.XXXXyjWkHw" 2L, 83C writtencrontab: installing new crontab

Edit Root Crontab entries

Login as root user (su – root) and do crontab -e as shown below.

root@dev-db# crontab -e

Edit Other Linux User’s Crontab File entries

To edit crontab entries of other Linux users, login to root and use -u {username} -e as shown below.

root@dev-db# crontab -u sathiya -e@monthly /home/sathiya/fedora/bin/monthly-backup00 09-18 * * * /home/sathiya/ubuntu/bin/check-db-status~~~"/tmp/crontab.XXXXyjWkHw" 2L, 83C

6. Schedule a Job for Every Minute Using Cron.

Ideally you may not have a requirement to schedule a job every minute. But understanding this example will will help you understand the other examples mentioned below in this article.

* * * * * CMD

The * means all the possible unit — i.e every minute of every hour through out the year. More than using this * directly, you will find it very useful in the following cases.

Page 4: Unix Cron Tab Features

When you specify */5 in minute field means every 5 minutes. When you specify 0-10/2 in minute field mean every 2 minutes in the first 10 minute. Thus the above convention can be used for all the other 4 fields.

7. Schedule a Background Cron Job For Every 10 Minutes.

Use the following, if you want to check the disk space every 10 minutes.

*/10 * * * * /home/ramesh/check-disk-space

It executes the specified command check-disk-space every 10 minutes through out the year. But you may have a requirement of executing the command only during office hours or vice versa. The above examples shows how to do those things.

Instead of specifying values in the 5 fields, we can specify it using a single keyword as mentioned below.

There are special cases in which instead of the above 5 fields you can use @ followed by a keyword — such as reboot, midnight, yearly, hourly.

Table: Cron special keywords and its meaningKeyword Equivalent

@yearly 0 0 1 1 *

@daily 0 0 * * *

@hourly 0 * * * *

@reboot Run at startup.

8. Schedule a Job For First Minute of Every Year using @yearly

If you want a job to be executed on the first minute of every year, then you can use the @yearly cron keyword as shown below.

This will execute the system annual maintenance using annual-maintenance shell script at 00:00 on Jan 1st for every year.

@yearly /home/ramesh/red-hat/bin/annual-maintenance

9. Schedule a Cron Job Beginning of Every Month using @monthly

It is as similar as the @yearly as above. But executes the command monthly once using @monthly cron keyword.

This will execute the shell script tape-backup at 00:00 on 1st of every month.

@monthly /home/ramesh/suse/bin/tape-backup

10. Schedule a Background Job Every Day using @daily

Using the @daily cron keyword, this will do a daily log file cleanup using cleanup-logs shell scriptat 00:00 on every day.

Page 5: Unix Cron Tab Features

@daily /home/ramesh/arch-linux/bin/cleanup-logs "day started"

11. How to Execute a Linux Command After Every Reboot using @reboot?

Using the @reboot cron keyword, this will execute the specified command once after the machine got booted every time.

@reboot CMD

12. How to Disable/Redirect the Crontab Mail Output using MAIL keyword?

By default crontab sends the job output to the user who scheduled the job. If you want to redirect the output to a specific user, add or update the MAIL variable in the crontab as shown below.

ramesh@dev-db$ crontab -lMAIL="ramesh"

@yearly /home/ramesh/annual-maintenance*/10 * * * * /home/ramesh/check-disk-space

[Note: Crontab of the current logged in user with MAIL variable]

If you wanted the mail not to be sent to anywhere, i.e to stop the crontab output to be emailed, add or update the MAIL variable in the crontab as shown below.

MAIL=""

13. How to Execute a Linux Cron Jobs Every Second Using Crontab.

You cannot schedule a every-second cronjob. Because in cron the minimum unit you can specify is minute. In a typical scenario, there is no reason for most of us to run any job every second in the system.

14. Specify PATH Variable in the Crontab

All the above examples we specified absolute path of the Linux command or the shell-script that needs to be executed.

For example, instead of specifying /home/ramesh/tape-backup, if you want to just specify tape-backup, then add the path /home/ramesh to the PATH variable in the crontab as shown below.

ramesh@dev-db$ crontab -l

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/ramesh

@yearly annual-maintenance*/10 * * * * check-disk-space

[Note: Crontab of the current logged in user with PATH variable]

Page 6: Unix Cron Tab Features

15. Installing Crontab From a Cron File

Instead of directly editing the crontab file, you can also add all the entries to a cron-file first. Once you have all thoese entries in the file, you can upload or install them to the cron as shown below.

ramesh@dev-db$ crontab -lno crontab for ramesh

$ cat cron-file.txt@yearly /home/ramesh/annual-maintenance*/10 * * * * /home/ramesh/check-disk-space

ramesh@dev-db$ crontab cron-file.txt

ramesh@dev-db$ crontab -l@yearly /home/ramesh/annual-maintenance*/10 * * * * /home/ramesh/check-disk-space

Note: This will install the cron-file.txt to your crontab, which will also remove your old cron entries. So, please be careful while uploading cron entries from a cron-file.txt.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Question: How do I execute certain shell script at a specific intervals in Linux using cron job? Provide examples using different time periods.

Answer: Crontab can be used to schedule a job that runs on certain internal. The example here show how to execute a backup.sh shell script using different intervals.

Also, don’t forget to read our previous crontab article that contains 15 practical examples, and also explains about @monthly, @daily, .. tags that you can use in your crontab.

1. Execute a cron job every 5 Minutes

The first field is for Minutes. If you specify * in this field, it runs every minutes. If you specify */5 in the 1st field, it runs every 5 minutes as shown below.

*/5 * * * * /home/ramesh/backup.sh

Note: In the same way, use */10 for every 10 minutes, */15 for every 15 minutes, */30 for every 30 minutes, etc.

2. Execute a cron job every 5 Hours

The second field is for hours. If you specify * in this field, it runs every hour. If you specify */5 in the 2nd field, it runs every 5 hours as shown below.

0 */5 * * * /home/ramesh/backup.sh

Note: In the same way, use */2 for every 2 hours, */3 for every 3 hours, */4 for every 4 hours, etc.

Page 7: Unix Cron Tab Features

3. Execute a job every 5 Seconds

Cron job cannot be used to schedule a job in seconds interval. i.e You cannot schedule a cron job to run every 5 seconds. The alternative is to write a shell script that uses ‘sleep 5′ command in it.

Create a shell script every-5-seconds.sh using bash while loop as shown below.

$ cat every-5-seconds.sh#!/bin/bashwhile truedo /home/ramesh/backup.sh sleep 5done

Now, execute this shell script in the background using nohup as shown below. This will keep executing the script even after you logout from your session. This will execute your backup.sh shell script every 5 seconds.

$ nohup ./every-5-seconds.sh &

4. Execute a job every 5th weekday

This example is not about scheduling “every 5 days”. But this is for scheduling “every 5th weekday”.

The 5th field is DOW (day of the week). If you specify * in this field, it runs every day. To run every Friday, specify either 5 of Fri in this field.

The following example runs the backup.sh every Friday at midnight.

0 0 * * 5 /home/ramesh/backup.sh(or)0 0 * * Fri /home/ramesh/backup.sh

You can either user number or the corresponding three letter acronym for the weekday as shown below.

0=Sun 1=Mon 2=Tue 3=Wed 4=Thu 5=Fri 6=Sat

Note: Get into the habit of using Fri instead of 5. Please note that the number starts with 0 (not with 1), and 0 is for Sun (not Mon).

5. Execute a job every 5 months

There is no direct way of saying ‘every 5 months’, instead you have to specify what specific months you want to run the job. Probably you may want to run the job on 5th month (May), and 10th month (Oct).

The fourth field is for Months. If you specify * in this field, it runs every month. To run for the specific month, you have to specify the number that corresponds to the month. For example, to run the job on May and Oct, you should specify 5,10 (or) you can simply use the 3 letter acronym of the month and

Page 8: Unix Cron Tab Features

specify May,Oct.

The third field is for DOM (Day of the Month). If you specify * in this field, it runs every day of the month. If you specify 1 in this month, it runs 1st of the month.

The following example runs the backup.sh twice a year. i.e 1st May at midnight, and 1st Oct at midnight.

0 0 1 5,10 * /home/ramesh/backup.sh(or)0 0 1 May,Oct * /home/ramesh/backup.sh

Note: Don’t make the mistake of specifying 5-10 in the 4th field, which means from 5th month until 10th month. If you want only 5th and 10th month, you should use comma.

How To Install, Edit, or Remove Cron Jobs in Batch Mode

Question: How can I install all the schedule jobs from a text file to the crontab? Also, can I remove all the cron jobs at once instead of removing the individual lines from the crontab?

Answer: You can install, edit and remove crontab in batch mode as examples below. Also, refer to our 15 crontab examples.

1. Install Crontab in Batch Mode

By specifying the file name as an argument to crontab command, you can install the new cron jobs from a text file as shown below.

First create a text file with all your cron job entries.

$ cat cron-file.txt* * * * * /bin/date >> /tmp/date-out* * * * * /bin/ls >> /tmp/ls-out

Next, install the cron jobs from a text file as shown below.

$ crontab cron-file.txt

Note: This will overwrite the existing cron entries.

2. Edit crontab in Batch Mode

You can edit the crontab in batch mode using various methods (for example, using sed).

Example: Change output redirection from write to append for all cron jobs.

$ crontab -l* * * * * /bin/date > /tmp/date-out* * * * * /bin/ls > /tmp/ls-out

$ crontab -l | sed 's/>/>>/' | crontab -

$ crontab -l* * * * * /bin/date >> /tmp/date-out* * * * * /bin/ls >> /tmp/ls-out

Page 9: Unix Cron Tab Features

3. Remove All cron jobs of the Current User

Crontab’s -r option removes all cron job for the current user. If you have appropriate privilege, you can even remove other user’s cron jobs using the -r option along with the -u user option.

Example: Remove the current user cron entries.

$ crontab -r

Example: Remove the specified user cron entries.

$ crontab -r -u USERNAME

Understand at, atq, atrm, batch Commands using 9 Examples

You can execute batch jobs in UNIX / Linux using any one of the three commands — at, batch or cron.

In this article, let us review how to schedule a job, view a job, and delete a job using at command.

You can schedule an at job in two different ways:

Schedule the job to be executed at a specific time. For example, July 3rd, 10AM Schedule the job to be executed in relative time from now. For example, 5 hours from now.

1. Schedule an at job using specific date and time

Syntax:

$ at time date

For example, to schedule a job at 11 am on May 20, use the following at command.

$ at 11 am may 20

2. Schedule an at job using relative time

You can schedule a job to be executed using relative time from now.

Syntax:

$ at now + COUNT UNIT

For example, following job will be execute 1 minute from now.

$ at now + 1 min

The above example will read the commands from stdin, and it will execute the job after a minute. When you give something wrong in time format, you will get the error ‘Garbled time‘.

You can schedule a background job for 1 hour from now, (or) 1 day from now using the following at command:

$ at now + 1 hour

Page 10: Unix Cron Tab Features

$ at now + 1 day

Similar to at command, you can also use crontab to execute jobs at a scheduled time. Refer to our earlier 15 cron command examples article.

3. View all the scheduled at jobs using atq

You can use atq command (or at -l), to display all the at command jobs that are scheduled or currently running.

The following atq command will list all the pending at jobs. The first number shown is the Job number, followed by the time in which the process is to be executed, and the user name.

$ atq4 2010-04-20 11:00 a sathiya

4. Remove/Delete a scheduled at job using atrm

You can use atrm command (or at -d), to delete a particular job. For example, to delete the job number 4, use the following atrm command.

$ atrm 4

5. Execute a job only when system load average is < 1.5 using batch command

You can schedule a job using batch command, which will prompt for command input, which will be executed when the system load average is less than 1.5.

$ batch

At the successful completion of input, you will get job number. For listing and removing batch jobs you can use the at commands explained above.

6. Schedule at jobs from file using -f option

First create a text file that contains all the commands, or shell-scripts that you would like to be executed in the background using at command.

$ cat myjobs.txt/home/sathiya/calculate-space.sh/path/to/a/shell-script/path/to/any/command/or/script

Using the -f option, you can make the at command to get the input from the file instead of stdin.

Following at command will execute all the jobs from the myjobs.txt 1 hour from now.

$ at -f myjobs.txt now + 1 hour

7. Allowing and Denying certain users from using at jobs

System administrator can control who can schedule an at job and who cannot using at.allow and

Page 11: Unix Cron Tab Features

at.deny files.

First, system checks for at.allow file. If at.allow exists, only the usernames specified in the at.allow file are allowed to use at command.

Next, (if at.allow doesn’t exist), system checks for at.deny file. If at.deny exist, the usernames specified in the at.deny file are not allowed to use the at command.

By default, most systems uses at.deny file to stop certain users from using the at command, such as www-data, guest, backup, man user.

8. Execute at command like nohup

Similar to the nohup command we discussed earlier, you can execute a command (or shell script) on the remote server using the at command and logout from the server.

$ at -f myjob now + 1 min

$ exit

Note: myjob will still be running even after you exit out of the server.

9. Additional at command time formats

You can use any one of the following at command date time formats:

$ at 10 am tomorrow

$ at 11:00 next month

$ at 22:00 today

$ at now + 1 week

$ at noon

6 Linux Crontab Command Examples

Crontab command manages the cron table that is used by the cron daemon to execute the cron jobs. This article explains the various command line options of the crontab command.

1. Tweaking Other Users Crontab using Option -u

-u stands for user. This should be followed by a valid username in the system. -u option alone doesn’t do anything. It should be combined with other options. Actually, it can be combined with any other crontab command line options.

Page 12: Unix Cron Tab Features

If you don’t specify -u username, crontab commands wil be executed on the current user. For example, all of the following crontab commands will be executed on the current logged in user.

crontab -lcrontab -ecrontab -r..

If you specify -u username, the crontab command will be executed on the given username. For example, all of the following crontab commands will be execute on the oracle user.

crontab -u oracle -lcrontab -u oracle -ecrontab -u oracle -r..

2. Display Cron Table using Option -l

-l stands for list. This displays the crontab of the current user. Since I’m logged in as root, this will display the cron jobs of root user.

# crontab -l53 00 * * 7 /bin/sh /home/root/bin/server-backup

To display the cron jobs of other users, combine -l with -u option.

# crontab -u oracle -l01 00 * * * /bin/sh /home/oracle/bin/rman-backup

The 15 crontab examples explains practical ways of using the cron job entries.

3. Edit Cron Table using Option -e

-e stands for edit. This allows you to edit the crontab of the current user. Since I’m logged in as root, this will automatically open root’s cron jobs in a Vim editor, and allow me to edit it.

# crontab -e53 00 * * 7 /bin/sh /home/root/bin/server-backup~~/tmp/crontab.7dgqju

As you notice from the above, /tmp/crontab.7dgqju is a temporary file created by the crontab automatically where you can edit your cron jobs.

When you save your edits and come out of the Vim editor, it will display oone of the following messages, depending on whether you made any changes or not.

# crontab -ecrontab: no changes made to crontab

# crontab -ecrontab: installing new crontab

Page 13: Unix Cron Tab Features

Note: The editor that crontab uses to open the cron jobs for editing depends on the VISUAL or EDITOR environment variable. By default, it will use Vim editor on Linux environment. But you can change it using the VISUAL/EDITOR environment variable.

To edit the cron jobs of other users, combine -e with -u option.

# crontab -u oracle -ecrontab: installing new crontab

To understand the meaning of the crontab entries itself, refer to How to Run a Cron Job Every 5 Minutes (or Hours, or Days, or Months).

4. Load Crontab from a File

Instead of manually editing the crontab to add new jobs, you can also upload all the cron jobs from a file. This is helpful when you have to maintain lot of servers that has the same cron job entries.

In the following example, all the cron jobs are in the /home/root/mycronjobs.txt file.

# cat /home/root/mycronjobs.txt53 00 * * 7 /bin/sh /home/root/bin/server-backup01 00 * * * /bin/sh /home/root/bin/check-user-quota

To upload the mycronjobs.txt jobs to current user crontab, do the following:

# crontab /home/root/mycronjobs.txt

Validate to make sure the cron jobs are successfully uploaded.

# crontab -l53 00 * * 7 /bin/sh /home/root/bin/server-backup01 00 * * * /bin/sh /home/root/bin/check-user-quota

Note: Be careful while using this upload method, as this will wipe-out all the current cron job entries before uploading the new ones.

To upload the cron job from a file to another user, combine it with -u option.

# crontab -u oracle /home/oracle/mycronjobs.txt

5. Add SELinux Security using Option -s

-s stands for SELinux. This will add the MLS_LEVEL variable to the crontab that contains the current SELinux security context.

To use -s option, you should upload the cron jobs from a file.

# cat /home/root/mycronjobs.txt53 00 * * 7 /bin/sh /home/root/bin/server-backup01 00 * * * /bin/sh /home/root/bin/check-user-quota

# crontab -s /home/root/mycronjobs/my.txtSELINUX_ROLE_TYPE=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c102353 00 * * 7 /bin/sh /home/root/bin/server-backup01 00 * * * /bin/sh /home/root/bin/check-user-quota

Page 14: Unix Cron Tab Features

Depending on your system the above will add either SELUNUX_ROLE_TYPE variable or MLS_LEVEL variable that contains the SELinux security context string. If you are not using SELinux in your environment, don’t worry about what this option does. SELinux is a separate topic of discussion, that we might cover in detail in future articles.

6. Delete All Cron Jobs using Option -r

-r stands for remove. This will remove all the cron job entries of the current user as shown below.

# crontab -l53 00 * * 7 /bin/sh /home/root/bin/server-backup01 00 * * * /bin/sh /home/root/bin/check-user-quota

# crontab -r

# crontab -lno crontab for root

-i stands for interactive mode. Combining -i with -r will ask you a confirmation before removing all the crontab entries.

# crontab -ircrontab: really delete root's crontab? n

To remove the cron jobs of other users, combine -r with -u option.

# crontab -u oracle -l01 00 * * * /bin/sh /home/oracle/bin/rman-backup

# crontab -u oracle -r

# crontab -u oracle -lno crontab for oracle

Cron Vs Anacron: How to Setup Anacron on Linux (With an Example)

Anacron is the cron for desktops and laptops.

Anacron does not expect the system to be running 24 x 7 like a server.

When you want a background job to be executed automatically on a machine that is not running 24 x 7, you should use anacron.

For example, if you have a backup script scheduled everyday at 11 PM as a regular cron job, and if your laptop is not up at 11 PM, your backup job will not be executed.

Page 15: Unix Cron Tab Features

However, if you have the same job scheduled in anacron, you can be sure that it will be executed once the laptop come back up.

Anacrontab Format

Just like how cron has /etc/crontab, anacron has /etc/anacrontab.

/etc/anacrontab file has the anacron jobs mentioned in the following format.

period delay job-identifier command

Field 1 is Recurrence period: This is a numeric value that specifies the number of days.

1 – daily 7 – weekly 30 – monthly N – This can be any numeric value. N indicates number of days

Note: You can also use ‘@monthly’ for a job that needs to be executed monthly.

Field 2 is Delay: This indicates the delay in minutes. i.e X number of minutes anacron should wait before executing the job after the the machine starts.

Field 3 is Job identifier: It is the name for the job’s timestamp file. It should be unique for each job. This will be available as a file under the /var/spool/anacron directory. This file will contain a single line that indicates the last time when this job was executed.

# ls -1 /var/spool/anacron/test.dailycron.dailycron.monthlycron.weekly

# cat /var/spool/anacron/test.daily20110507

Field 4 is command: Command or shell script that needs to be executed.

Just like shell scripts, comments inside anacrontab file starts with #

Note: For /etc/crontab file format, refer to our Linux Crontab: 15 Awesome Cron Job Examples article.

Anacron Example

The following example executes the /home/sathiya/backup.sh script once in every 7 days.

On the day when the backup.sh job is supposed to executed, if the system is down for some reason, anacron will execute the backup.sh script 15 minutes after the system comes back up (without having to wait for another 7 days).

# cat /etc/anacrontab7 15 test.daily /bin/sh /home/sathiya/backup.sh

Page 16: Unix Cron Tab Features

START_HOURS_RANGE and RANDOM_DELAY

The above example indicates that the backup.sh script should be executed every day, with a delay of 15 mins. i.e When the laptop was started, executed it only after 15 minutes.

What happens when the laptop or desktop was not shutdown? When does the job gets executed? This is specified by the START_HOURS_RANGE environment variable in the /etc/anacrontab file.

By default this is set to 3-22 in the file. This indicates the time range from 3 a.m to 10 p.m.

# grep START /etc/anacrontabSTART_HOURS_RANGE=3-22

On top of the user defined delay specified in the 2nd field of the /etc/anacrontab file, anacron also randomly adds x number of minutes. The x is defined by the RANDOM_DELAY variable in the /etc/anacrontab file.

By default this is set to 45 in the file. This means that anacron will add x minutes (randomly picked from 0 and 45), and add this to the user defined delay.

# grep RANDOM /etc/anacrontabRANDOM_DELAY=45

Cron Vs Anacron

Cron and anacron has its own advantages and disadvantages. Depending on your requirement, use one of them.

Cron Anacron

Minimum granularity is minute (i.e Jobs can be scheduled to be executed every minute)

Minimum granularity is only in days

Cron job can be scheduled by any normal user ( if not restricted by super user )

Anacron can be used only by super user ( but there are workarounds to make it usable by normal user )

Cron expects system to be running 24 x 7. If a job is scheduled, and system is down during that time, job is not executed.

Anacron doesn’t expect system to be running 24 x 7. If a job is scheduled, and system is down during that time, it start the jobs when the system comes back up.

Ideal for servers Ideal for desktops and laptops

Use cron when a job has to be executed at a particular hour and minute

Use anacron when a job has to be executed irrespective of hour and minute