Section 4.7. Scheduling Commands: crontab and at


[Page 128 (continued)]

4.7. Scheduling Commands: crontab and at

There are two utilities that allow you to schedule commands to be executed at a later point in time:

  • crontab, which allows you to create a scheduling table that describes a series of jobs to be executed on a periodic basis

  • at, which allows you to schedule jobs to be executed on a one-time basis

The subsections that follow describe each utility.

4.7.1. Periodic Execution: Crontab

The crontab utility allows you to schedule a series of jobs to be executed on a periodic basis and works as described in Figure 4-17.

Figure 4-17. Description of the crontab command.

Utility: crontab crontabName

crontab [ -u userName ] -ler

crontab is the user interface to the Linux cron system. When used without any options, the crontab file called crontabName is registered and its commands are executed according to the specified timing rules. The -l option lists the contents of a registered crontab file. The -e option edits and then registers a registered crontab file. The -r option removes a registered crontab file. The -l, -e, and -r options may be used by a super-user to access another user's crontab file by using the -u option. The anatomy of a crontab file is described shortly.


To use crontab, you must prepare an input file that contains lines of the format:

minute  hour  day  month  weekday  command 


where the values of each field are as shown in Figure 4-18.

Figure 4-18. crontab field meanings and values.
(This item is displayed on page 129 in the print version)

Field

Valid value

minute

059

hour

023

day

131

month

112

weekday

17 (1 = Mon, 2 = Tue, 3 = Wed, 4 = Thu, 5 = Fri, 6 = Sat,7 = Sun, but 0 can be used for Sunday as well) or the name of the day

command

any Linux command


Files of this nature are called "crontab" files. Whenever the current time matches a line's description, the associated command is executed by /bin/sh (which is Bash on a Linux system). If any of the first five fields contain an * instead of a number, the field always matches. The standard output of the command is automatically sent to the user via mail. Any characters following a % are copied into a temporary file and used as the command's standard input. Here is a sample crontab file that I created in my home directory and called "crontab.cron":


[Page 129]

$ cat crontab.cron            ...list the crontab file. 0  8  *  *  1-5   echo Welcome to work *  *  *  *  *     echo One Minute Passed > /dev/pts/1 30 14 1  1,4,7,10  *   mail users % Quarterly Meeting At 3pm $ _ 


The first line causes the message "Welcome to work" to be mailed to me at 8 a.m. every weekday morning. The next line echoes "One Minute Passed" every minute to the device "/dev/pts/1", which happens to be my terminal. The last line sends mail to all users on the first day of January, April, July, and October, at 2:30 p.m. to remind them of an impending meeting.

There is a single process called "cron" (or sometimes "crond") that is responsible for executing the commands in registered crontab files in a timely fashion. It is started when the Linux system is booted and does not stop until the Linux system is shut down. Copies of all registered crontab files are stored in the directory "/var/spool/cron" in a file named the same as the username.

To register a crontab file, use the crontab utility with the name of the crontab file as the single argument:

$ crontab crontab.cron      ...register the crontab file. $ _ 


If you already have a registered crontab file, the new one is registered in place of the old one. To list the contents of your registered crontab, use the -l option. To list someone else's, add their name as an argument. Only a super-user can use this option. In the example that follows, note that one of my previously registered crontab file entries triggered coincidentally after I used the crontab utility.


[Page 130]

$ crontab -l    ...list contents of current crontab file. 0  8  *  *  1-5   echo Welcome to work *  *  *  *  *     echo One Minute Passed > /dev/pts/1 30 14 1  1,4,7,10  *  mail users % Quarterly Meeting At 3pm $ One Minute Passed   ...output from one crontab command. 


To edit your crontab file and then resave it, use the -e option. To remove a registered crontab file, use the -r option:

$ crontab -r              ...remove my crontab file. $ _ 


A super-user may create files called "/etc/cron.allow" or "/etc/cron.deny" to either enable only specific users or prevent individual users from using the crontab facility. Each file consists of a list of user names on separate lines. If neither of the files exist, all users may use crontab.

4.7.2. One-Time Execution: at

The at utility allows you to schedule one-time commands or scripts (Figure 4-19).

Figure 4-19. Description of the at command.

Utility: at -csm time [ date [, year ] ] [ +increment ] [ -f script ]

atrm { jobId }+

atq

at allows you to schedule one-time commands and/or scripts. It supports a flexible format for time specification. If the -f option is specified, commands will be taken from the file script, otherwise commands are read from stdin. Commands are run by /bin/sh, which on Linux is Bash. If no script name is specified, at takes a list of commands from standard input. The output from the script is sent to the user via e-mail when the job is complete. The -m option instructs at to send you mail even if there is no output. atrm removes the specified jobs from the at queue. The atq command and lists the pending jobs. A job is removed from the at queue after it has executed.

time is in the format HH or HHMM followed by an optional A.M./P.M. specifier, and date is spelled out using the first three letters of the day and/or month. The keyword "now" may be used in place of the time sequence. The keywords "today" and "tomorrow" may be used in place of date. If no date is supplied, then at uses the following rules:

  • If time is after the current time, then date is assumed to be "today".

  • If time is before the current time, then date is assumed to be "tomorrow".

The stated time may be augmented by an increment, which is a number followed by "minutes," "hours," "days," "weeks," "months," or "years".



[Page 131]

In the following example, I scheduled an at job to send a message to my terminal device.

$ cat at.sh       ...look at the script to be scheduled. echo at done > /dev/pts/1       ...echo output to terminal. $ date                          ...look at current time. Sat Jan 22 17:30:42 CST 2005 $ at now + 2 minutes -f at.sh   ...schedule script to ... execute in 2 minutes job 2519 at 2005-01-22 17:32 $ atq                      ...look at the at schedule. 2519    2005-01-22 17:32 a ables $ _ at done                   ...output from scheduled script. $ at 17:35 -f at.sh       ...schedule the script again. job 2520 at 2005-01-22 17:35 $ atrm 2520                   ...deschedule. $ atq                         ...look at the at schedule. $ _ 


Here are some more examples of legal at time formats:

0934am Sep 18 9:34 Sep 18, 2005 11:00pm tomorrow now + 1 day 9pm Jan 13 10pm Wed 


If you omit the command name, at displays a prompt and then waits for a list of commands to be entered from standard input. To terminate the command list, press a Control-D. Here's an example:

$ at 8pm     ...enter commands to be scheduled from keyboard. at> echo at done > /dev/pts/1 at>^D             ...end-of-input. job 2530 at 2005-01-22 20:00 $ _ 


You may program a script to reschedule itself by calling at within the script:

$ cat at2.sh        ...a script that reschedules itself. date > /dev/pts/1 # Reschedule script at now + 2 minutes -f at2.sh $ _ 


A super-user may create files called "/etc/at.allow" or "/etc/at.deny" to enable and inhibit individual users from using the at facility. Each file should consist of a list of user names on separate lines. If neither file exists, all users may use at.




Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net