Introduction to Unix

../_images/ksuPoly.jpg ../_images/UNIX_blocks.jpeg

3.7. Scheduling Commands

Linux for Programmers and Users, Section 4.7.

You may want to schedule some programs to run at later time or want them to run on a regular, repeating schedule. Examples of when you might want to do this are:

  • If you have a program that uses a lot of the computer’s resources, you may want it to run at time when you will not be using the computer.
  • To run system maintenance tasks, such as making backups or scanning the filesystem for viruses or other security concerns.

Unix has a facility for running scheduled tasks called cron, but users do run cron directly. It is always running in the background to run scheduled commands at the appropriate times. We call system programs, such as cron, that run in the backgroud daemons.

Note

The output from any scheduled commands, is by default sent to the user in an e-mail message. If this is not desired, then when scheduling the command, redirect the output from the command to a file or to the system provided trash can (/dev/null).

If you receive an e-mail message from the cron daemon, you might try using mutt, to read the message.

The programs that users use to schedule programs are crontab and at.

3.7.1. crontab

SYNOPSIS

crontab [-u user] [file]

crontab [-u user] -l | -r [-i] | -e

DESCRIPTION

Crontab is the program used to install, deinstall or list the tables used to drive the cron daemon for running commands on a repeating schedule.

The first usage is to install a schedule of commands from a file or from standard input. If no options are specified, it reads from standard input.

-u user

Specify which user’s schedule to adjust (only for root). The default is for your own account.

-l

List the currently scheduled commands

-r

Remove the currently scheduled commands

-i

Prompt the user to confirm removal of the scheduled commands

-e

Edit the list of scheduled commands. After saving the temporary file and exiting from the editor, the commands entered in the file are scheduled. The default editor used is vi. If you wish to use another editor, such as nano, set your EDITOR environment variable:

export EDITOR='nano'

crontab file format

  • Each scheduled command should be on one line of the file.
  • The file should contain 6 fields separated by spaces.
  • The first five field specify when the command should run (minute, hour, day, month, weekday). A wild-card (*) may used to specify every instance. [1] A specific time value, a list of times or a range of times may be used for any field.
    • The hour is in 24 hour time format (0-23).
    • The valid values for weekday are 0 to 7. Sunday is both 0 and 7. Monday to Saturday take the values 1 to 6.
  • The last field specifies the command to run along with any arguments or redirection of the output. For some commands, it may be advisable to list the absolute path name of command rather than relying

3.7.2. at

SYNOPSIS

at -r job | -l | TIME

DESCRIPTION

The at command is used to schedule a command to run one time.

-r job

Remove scheduled jobs, identified by their job number. This is the same as running atrm job

-l

List the scheduled jobs

TIME

TIME is a very flexible specification of when the command should run. See the text book for examples. at then reads from standard input for list of commands to run at this time. Type the EOF character (Cntrl-d) when finished entering commands.

Footnotes

[1]Give some though to where you might want to use a wildcard (*) character. A wildcard in the day field, means that that the day of the month is not important – other criteria such weekday will determine when the program runs. However, a wildcard in the minute field would mean that for the specified hour, the command runs once for every minute (60 times).