Scheduling Jobs

You schedule one off tasks in Linux using the at command and recurring jobs using the crontab command.


To schedule a one off job type at at the command prompt followed by the time when you want the command to run. For example

 at 19:00
at now+15 minutes
at May 1 07:00

In the first instance the job will be scheduled for 7pm tonight (or tomorrow night if it is already after 7pm). In the second instance the job will run fifteen minutes after you press enter. In the third instance the job will run at 7am on 1st May.

After you enter the at command the at prompt will be returned
You then enter the command(s) to run the job(s) that you want to run just as if you were going to run them immediately. These commands will then be stored to execute at the specified date and time.

Once you have entered all of the details to run the job(s) hold down the ctrl key and press the D key to exit from the at command.

You can check to see what one off tasks are scheduled using the atq command and then remove any that you no longer require using the atrm command. The atrm command requires that you specify the job number of the job to be deleted. You can find out the job number of a job to run at a specific time using the atq command. Job numbers are unique to the user who created them.


Setting up to run the same task at regular intervals is somewhat more involved. In this instance you need to set up a small text file containing the instructions to be executed identifying when each is to be run (eg. mycronfile) and then submit the file to the system using the crontab command (eg. crontab mycronfile).

You can find examples of the layout of the file that you need to create on the man crontab pages. The file can contain environment variables such as SHELL= (which identifies which command shell to use) and MAILTO= (which identifies where to send the results). The file must also contain the commands that are to be executed on a regular basis.

Each command is preceded by five fields which identify the minute (0 - 59), hour (0 - 23), day (1 - 31), month (1 - 12 or first three characters), and day of the week (0 - 7 where 0 and 7 both = Sunday or first three characters) on which the command is to be executed. Each of these fields may also be set to asterisk (*) which means that all possible values will be used for the field. Multiple values may also be specified by separating them with commas. The following are some examples of these field combinations and what they mean:

1 0 * * * means run at one minute after midnight every day
0 12 1 * * means run at midday on the first of each month
15 18 * * 1 means run at 6:15pm every Monday
0 12 1 2,4,6,8,10,12 * means run at midday on the first of February, April, June, August, October, and December

In each case these five fields are then followed by the command that is to be executed at the specified date/time intervals.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow