Linux crontab examples

Scheduling tasks on Linux

Crontab is a program that is used by system administrators and users to schedule the running of jobs at a specified or repeated time. Crontab creates tables that the cron daemon uses for 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 an example I have created a crontab that will run the command "ls -ld /tmp > /home/john/mycront.txt" at 21:15hrs on Monday 18th February"


$ crontab -e
no crontab for john - using an empty one
crontab: installing new crontab


$ crontab -l

15 21 18 2 1 ls -ld /tmp > /home/john/mycront.txt

To prove my crontab entry worked I have checked the output in "/var/log/syslog for my entry:


Feb 18 21:13:41 john-desktop crontab[3222]: (john) LIST (john)
Feb 18 21:15:01 john-desktop CRON[3231]: (john) CMD (ls -ld /tmp > /home/john/mycront.txt)

And as a final check, I can see that the file "mycront.txt" was created in my home directory:


$ ls -l myc*
-rw-rw-r-- 1 john john 48 Feb 18 21:15 mycront.txt

$ 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 time 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.

Crontab Examples

Minute Hour Day of Month Month Day of Week Examples
30 18 1 1,6,12 * Example 1
0 20 * 6 1-5 Example 2
*/10 * * * * Example 3

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

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:

The location of the directories below can be found under the "/etc" area.


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.