CLASS="CHAPTER" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" >

Chapter 18. Scheduling Commands to run in the background

There are two main tools used to perform scheduled tasks, at and cron. You may also like to try anacron if your computer does not run continuously, as cron will only work if your computer is left on (anacron can catch up with the scheduled tasks the next time the computer is on...).

at

'at' executes a command once on a particular day, at a particular time. at will add a particular command to be executed.

Examples:

at 21:30

You then type the commands you want executed then press the end-of-file key (normally CTRL-D ). Also try:

at now + time

This will run at the current time + the hours/mins/seconds you specify (use at now + 1 hour to have command(s) run in 1 hour from now...)

You can also use the -f option to have at execute a particular file (a shell script).

at -f shell_script now + 1 hour

This would run the shell script 1 hour from now.

atq

Will list jobs currently in queue for the user who executed it, if root executes at it will list all jobs in queue for the at daemon. Doesn't need or take any options.

atrm

Will remove a job from the 'at' queue.

Command syntax:

atrm job_no

Will delete the job “job_no” (use atq to find out the number of the job)

cron

cron can be used to schedule a particular function to occur every minute, hour, day, week, or month.

It's normal to use the crontab to perform the editing functions as this automates the process for the cron daemon and makes it easier for normal users to use cron.

TipAnacron
 

anacron is another tool designed for systems which are not always on, such as home computers

While cron will not run if the computer is off, anacron will simply run the command when the computer is next on (it catches up with things).

crontab

crontab is used to edit, read and remove the files which the cron daemon reads.

Options for crontab (use crontab -option(s)):

  • -e --- to edit the file.

  • -l --- to list the contents of the file.

  • -u username --- use the -u with a username argument to work with another users crontab file.

When using crontab -e you have a number of fields (6) what they mean is listed below:

Field

Allowed Values

minute

0-59

hour

0-23

day of month

1-31

month

1-12 (or names, see below)

day of week

0-7 (0 or 7 is Sun, or use three letter names)

There are also a number of shortcut methods for common tasks, including:[1]

  • @reboot --- run command at reboot

  • @yearly --- same as 0 0 1 1 *

  • @annually --- same as @yearly

  • @monthly --- same as 0 0 1 * *

  • @weekly --- same as 0 0 * * 0

  • @daily --- same as 0 0 * * *

  • @midnight --- same as @daily

  • @hourly --- same as 0 * * * *

[2]

Note that * (asterisk) is used to mean anything (similar to the wildcard). For example if you leave the day part (the 5th place) with an asterisk it would mean everyday.

Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: ``1,2,5,9'', ``0-4,8-12”.

Step values can be used in conjunction with ranges. Following a range with ``/<number>'' specifies skips of the number's value through the range. For example, ``0-23/2'' can be used in the hours field to specify command execution every other hour (the alternative in the V7 standard is ``0,2,4,6,8,10,12,14,16,18,20,22''). Steps are also permitted after an asterisk, so if you want to say ``every two hours'', just use ``*/2''.

When writing a crontab entry you simply type in six fields separated by spaces, the first five are those listed in the table (using numbers or letters and numbers as appropriate), the 6th field is the command to be executed and any options, cron will read everything up until the newline.

Example:

5 4 * * sun echo "run at 5 after 4 every sunday"

This would run the echo command with the string shown at 4:05 every Sunday.

Notes

[1]

This information has come from (without editing) a post on the LinuxChix techtalk mailing list, please see [16] in the Bibliography for further information.

[2]

This information comes from the cron manual page with small additions (no changes to original content), refer to [13] in the Bibliography for further information.