Scheduled Activity: cron and at
The Linux utilities cron and at are related commands. The cron utility allows you to schedule a repetitive task to take place at any regular interval desired, and the at command lets you specify a one-time action to take place at some desired time. You might use crontab, for example, to perform a backup each morning at 2 a.m., and use at to remind yourself of an appointment later in the day.
The word “crontab” is a Unixism for “chron table”, or time table. You create a table in the required format, specifying commands and times to execute the commands. Commands you put in the table can be any executable programs—for example, a command in /usr/bin or a shell script you wrote. You use the crontab command to create, edit, or list the table, and the system cron daemon reads the table and executes the commands at the times specified.
The cron daemon is normally executed at system startup and does not exit. On my Linux system, the cron daemon is actually Matthew Dillon's crond, and is started in /etc/rc.d/rc.M with the following line:
/usr/sbin/crond -l10 >/var/adm/cron 2>&1
On some Linux systems, Paul Vixie's cron daemon is used, in which case the name of the daemon is simply cron. Also, on systems with newer versions of init, cron is started from the /etc/init.d/cron script.
You can check to see if a cron daemon is running on your system with a command such as the following:
$ ps -ax | grep cron raithel 733 pp0 S 0:00 grep cron root 25 ? S 0:00 /usr/sbin/crond -l10
In this case, we see that the crond daemon is indeed running.
When the cron daemon starts, it reads the various crontab tables in the crontab directory, normally /usr/spool/cron/crontabs. To create or change your crontab file, use crontab's -e option:
$ crontab -e
You are placed in a text editor with a copy of your current crontab file, if it exists, or a blank file, if it does not. The text editor you get is determined by the setting of your VISUAL environment variable (or EDITOR, if VISUAL is not set) and is usually the vi editor, if you have not specified otherwise.
To schedule commands with crontab, you must use the format cron recognizes in a crontab file. The format is not exactly mnemonic, so I create a crontab file with a header commented out that provides the necessary information:
# minute (0-59), # hour (0-23), # day of the month (1-31), # month of the year (1-12), # day of the week (0-6, 0=Sunday), # command
Each crontab entry is a single line composed of these six fields, separated by white space. Specify the minute a command is to be executed with the digits 0 through 59 in the first field, the hour with 0 through 23 in the second field, the day of the month with 1 through 31 in the third field, the month of the year with 1 through 12 in the fourth field, and the day of the week with 0 through 6 in the fifth field. Place the command to be executed in the sixth field.
At first glance it may appear that redundant or conflicting information is required because there are two “day” fields—day of the month and day of the week, but this is really just to permit different scheduling algorithms. For instance, you may want to be reminded to attend a meeting every Tuesday or to pick up your paycheck every 15th of the month. Enter an asterisk (*) in the day field you are not using. You can use both day fields if you prefer to have the command execute on, say, the fifteenth of the month as well as every Tuesday.
Ranges are specified with a dash. If you want to specify the eighth through the fifteenth days of the month, enter 8-15 in the third field. Non-consecutive entries in a field are separated by commas, so 1,15 in the third field means the first and fifteenth of the month. To specify all values for a field, for example every month of the year, enter an asterisk (*) in the field. (Note that to specify every day you must enter * in both day fields.)
Here is an example crontab file with two entries:
# minute (0-59), # hour (0-23), # day of the month (1-31), # month of the year (1-12), # day of the week (0-6, 0=Sunday) # command 12 4 * * * /usr/local/bin/backup 5 3 10-15 4 * echo "taxes due" | mail jones
The first line after the comments causes a backup script to execute early each morning at 4:12 a.m., and the second line causes the user jones to get a mail message for six days in April as a reminder that taxes are due. In general, it's a good idea to execute crontab commands at off hours like these to reduce any effect on system load during normal usage hours.
If you don't specifically redirect standard error and standard output, they are mailed to you as owner of the crontab file when the command executes. In the example above, if the user jones cannot be found, you would be mailed the output as well as an error message.
After editing the crontab file, save it and exit from the editor. A file is created for you in the crontab directory. For example, the crontab for root is the file /usr/spool/cron/crontabs/root. This file is read by the system cron daemon and stored in an internal format, where it will remain to be periodically executed until it is changed or deleted.
To view your current crontab file, use the -l (for “list”) option:
$ crontab -l
To delete your file, use:
$ crontab -d
If you are superuser, you can delete any user's crontab file with:
# crontab -d username
where username is the user's login name.
The crontab commands discussed above work fine on my Linux system and should work on System V and BSD Unix systems, as well. One thing to be aware of when using crontab on other systems or moving crontab files to other systems is that some cron daemons allow the superuser to restrict crontab service by the creation of cron.allow and cron.deny files. Refer to the specific system documentation for details.
Also, most versions of cron provide an /etc/crontab file which has an extra field in it—the user as which to execute the command. Again, check the documentation for your version of cron for more details.
Use at when you want to execute a command or multiple commands once at some future time.
In Linux, the at command requires that the atrun command be started in root's crontab file. Many Linux distributions ship with at enabled, but some do not. To enable the at utility on your system, become superuser and edit root's crontab file:
$ su root Password: # crontab -e
and add the following line:
* * * * * directory/atrun
where directory is the location where the atrun executable is stored. On my system that's /usr/lib, so the entry is:
* * * * * /usr/lib/atrun
This causes atrun to be executed every minute. After a minute or so of adding the atrun line and saving the crontab file, any existing at commands are evaluated and executed if the time is right. (Before this, you may have been able to enter at commands, but they were never executed.)
To demonstrate the at command, let's have it print “hello” on your current terminal window after a few minutes. First, get the time and your current terminal device:
$ date Tue Oct 3 15:33:37 PDT 1995 $ tty /dev/ttyp2
Now run the at command. Specify a time in the command line, press Return, and then enter the command, followed by another Return and a Ctrl-D:
$ at 15:35 echo "hello" > /dev/ttyp2 ^D Job c00ceb20b.00 will be executed using /bin/sh
The at command takes input up to the end-of-file character (generated by pressing ctrl-D while at the beginning of a line.) It reports the job number and informs you that it will use /bin/sh to execute the command. In two minutes, hello should appear on the display of /dev/ttyp2. Note that you can enter a series of commands, one per line—at will read each line up to the end-of-file and execute the file as a /bin/sh shell script at the specified time.
Suppose you want to set an alarm. One way to tell at to do something is to use the relative form of timing, specifying a time relative to now. If you want your computer to beep at you in 25 minutes, enter:
$ at now + 25 minutes echo ^G > /dev/ttyp4 ^D Job c00ceb7fb.00 will be executed using /bin/sh
and you are beeped in 25 minutes. There is a great deal of flexibility allowed in entering time specifications. For example, at recognizes military time, “am” and “pm”, month abbreviations, time notation that includes the year, and so on. My at man page even claims at accepts teatime, noon, and other constructs. Refer to the at man page for more examples of valid time specifications.
You must tell at your tty location, or it won't send output to your terminal window. If you prefer, you can receive mail:
$ at 4:55pm Friday echo '5 p.m. meeting with Carol' | mail raithel ^D Job c00ceb7fb.01 will be executed using /bin/sh
To get a list of your pending at jobs, enter:
$ atq
If you are superuser, atq shows you the pending at jobs of all users. To delete a job, enter:
$ atrm job_number
where job_number is the job number returned by atq. The superuser can also remove other user's jobs.
The following is a simple script that makes it easier for me to use at to send myself reminders. The script sends mail to the user containing the message line(s) entered at the prompt at the time specified. It also displays some syntax examples of how to specify time, which I find a useful memory refresher.
Notice the script, as written, requires you to have a Msgs directory in your home directory. I created $HOME/Msgs, rather than using something like /usr/tmp, so the messages are more private until they are deleted by the script.
#!/bin/sh echo "Enter your reminder message. When finished, enter a period (.) at the beginning of a line and press Enter. (Or press Ctrl-C or DEL to exit.)" while : do read MESSAGE if [ "$MESSAGE" = "." ] then break else echo $MESSAGE > $HOME/Msgs/message.$$ fi done cat << !! Enter time and day you want to receive the message, for example: 0815am Jan 24 8:15am Jan 24 now + 1 day 5 pm Friday Then press Enter. !! read TIME echo \ "at $TIME mail $LOGNAME $HOME/Msgs/message.$$" at $TIME << !! mail $LOGNAME < $HOME/Msgs/message.$$ rm -f $HOME/Msgs/message.$$ !! exit 0
The user's interface to the crontab and at commands is very similar across different versions of Unix, but implementations of underlying directory structures, daemons, and access controls may differ. Be sure to review your system documentation to take advantage of all aspects of these powerful commands.
There's practically no limit to the use of crontab and at, but let me offer a few words of warning. First, consider security issues when enabling user crontab and at permissions. Obviously, a disgruntled co-worker could leave a “time-bomb” of some sort, limited only by his other permission restrictions. Many versions of crontab and at allow you to specify “allow” and “deny” files to control which users have access to the utilities. You could also use root's crontab to check for and remove user crontabs (or any other files) if you want to.
Also, debug your crontab file entries thoroughly. Check that they are working. These entries are usually scheduled to execute at times of low-usage, so it is unlikely you'll be around to observe them at the time.
John Raithel is a consulting technical writer specializing in documenting the system and network administration of the Unix operating system, and is currently working on World Wide Web and firewall documentation for Silicon Graphics, Inc. He lives in a small town on the central California coast where he plays with his Linux and SunOS “mini-network”. He can be reached via e-mail at raithel@rahul.net.