Scheduling tasks on Linux
Crontab is a program that is used by system administrators and users to schedule the running of jobs at specified or repeated times. Crontab creates tables that the cron daemon uses scheduling. Crontab files are created in the "/var/spool/cron/crontabs" area, however, these files are not intended to be edited directly. Editing is done via some special commands.
To use the cron functionality, you must have your userid present in the "/etc/cron.allow" file. If this file does not exist but the "/etc/cron.deny" file does , then you must not be present in the "/etc/cron.deny" file to use this command. If both of these files exist, then the allow file will take precedence over the deny file. By default the "root" user is always allowed to create a crontab.
There are four basic commands that can be used when working with crontab. These are:
crontab -e -Edits a crontab for current user. The crontab file is generated when exiting your editor.
crontab -l -Lists contents of crontab file for current user.
crontab -r -Removes crontab for current user.
crontab -u userid -You can specify a specify user by using the "-u" flag.
The basic structure of a crontab file
* * * * * Command to be Executed
- - - - -
| | | | |
| | | | +----- Day of Week (0 - 6) (Sunday=0)
| | | +------- Month (1 - 12)
| | +--------- Day of Month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minutes (0 - 59)
The basic syntax of a crontab consists of five fields: Minutes, Hour, Day of Month, Month, Day of Week and Command to be executed.
As a test I created a crontab that will run the command "ls -ld /tmp > /home/john/mycront.txt" at 21:15hrs on Monday 18th February"
john@john-desktop:~$ crontab -e
no crontab for john - using an empty one
crontab: installing new crontab
john@john-desktop:~$ crontab -l
15 21 18 2 1 ls -ld /tmp > /home/john/mycront.txt
To prove my crontab entry worked I checked the output in /var/log/syslog for my entry:
Feb 18 21:13:41 john-desktop crontab: (john) LIST (john)
Feb 18 21:15:01 john-desktop CRON: (john) CMD (ls -ld /tmp > /home/john/mycront.txt)
And as final proof, I can see the file mycront.txt was created in my home directory:
john@john-desktop:~$ ls -l myc*
-rw-rw-r-- 1 john john 48 Feb 18 21:15 mycront.txt
john@john-desktop:~$ cat mycront.txt
drwxrwxrwt 14 root root 20480 Feb 18 21:15 /tmp
In the above example, I specified a fixed time. However, it is possible to add multiple entries into each field:
15,30 21 18,19,20 2 * ls -ld /tmp > /home/john/mycront.txt
In the above example we are going to execute the same command at 21:15hrs and 21:30hrs on the 18th,19th and 20th of February.
More examples of crontab entries
|Minute||Hour||Day of Month||Month||Day of Week||Examples|
In Example 1 this entry would be executed at 18:30hrs on the 1st of January, 1st of June and 1st December
In Example 2 this entry is executed at 20:00hrs, Monday to Friday for the month of June
In Example 3 this entry would execute every 10 Minutes
Anacron is a scheduler similar in ways to that of cron. The main difference between cron and anacron is that anacron doesn't assume that your machine will be running 24 hours a day. Anacron offers system-wide directories that can run scripts on an hourly, daily, weekly or monthly basis. The scripts that you require to be executed can be placed inside these directories:
drwxr-xr-x 2 root root 4096 Feb 12 19:59 cron.daily
drwxr-xr-x 2 root root 4096 Jul 4 2012 cron.hourly
drwxr-xr-x 2 root root 4096 Jul 4 2012 cron.monthly
drwxr-xr-x 2 root root 4096 Jan 9 21:33 cron.weekly
All the scripts within these directories run as root. You can prefix the filename with a number to identify the order the scripts should be run in. More information regarding this feature can be found by issuing the "man run-parts" command.