4.7. Scheduling Commands: crontab and atThere are two utilities that allow you to schedule commands to be executed at a later point in time:
The subsections that follow describe each utility. 4.7.1. Periodic Execution: CrontabThe 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.
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.
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": $ 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. $ 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: atThe at utility allows you to schedule one-time commands or scripts (Figure 4-19).
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. |