Unix: Delayed execution


In addition to interactive use of the computer, Gnu/Linux offers several tools that let the user set up programs (and even a sequence of programs, known as “scripts”) to be run at some point in the future without manual intervention. Three of these tools are:


Cron is the name of time based scheduling framework that lets you schedule a single program or a script to be executed at a specified periodic time/date.

Most Gnu/Linux systems comes with Vixie cron (a version of the cron framework created by Paul Vixie). This is the version that will be described below. Other versions may require a different syntax, so please read up on the documentation that exists on your particular system.

To use the cron framework, you create a crontab, which is a list of entries. Each entry is a single line of text, and consists of a set of time fields indicator followed by a commnd.

There are several ways to create the crontab entries. I prefer to create an ordinary text file with all the entries I want in my crontab in a file named crontab.txt. I then can create an active crontab (or replace the current crontab with the contents of that file with the following shell command):

$ crontab crontab.txt

To list the content of your current active crontab, you log on to the host where your crontab is active, and type the following shell command:

$ crontab -l

The time fields for crontab entry are the following five fields, separated by spaces:

Field Values Comments
minute 0-59  
hour 0-23 0 is midnight
day of month 1-31  
month 1-12 1 is Jan, 12 is Dec
day of week 0-6 0 is Sun, 6 is Sat (on most systems)

These fields can contain a single number, a pair of numbers separated by a dash (i.e. a range of numbers), a comma-separated list of numbers and ranges, or an asterisk (* that represents all valid values for that field). A slash is used used in conjunction with ranges to specify step values. Some versions may also accept strings of letters: for instance, Vixie cron accepts month and day names instead of numbers.

For instance: The first of the following four crontab entries will run command1 at 13:00 (1 p.m.) every 9th of February. The second will run command2 at 15:05 (five minutes past 3 p.m.) every Monday. The third will run command3 at 05:00 (5 a.m.) every Tuesday plus the 1st through the 7th of every month, The fourth will run command4 every ten minutes. The fifth will, at 03:00 (3 a.m. in the morning) every night, run the find command to search for and remove all files below the file system root ending with .bak that have not been accessed in 7 days.

0 13 9 2 * command1
5 15 * * 1 command2
0 5 1-7 * 2 command3
*/10 * * * * command4
0 3 * * * find / -name "*. bak" -type f -atime +7 -exec rm {} \;

If the first character in a crontab entry is a hash mark (#), cron will treat the entry as a comment and ignore it. This is a quick way to disable an entry without deleting it.


Sometimes you may need to a program to run just once at one point in the future, rather than periodically. For this you use the at command.





Command Meaning
crontab maintain crontab files for individual users
at -
sleep -