Scheduling Regularly Occurring Jobs with cron
Suppose you want to send yourself a reminder message just before you go home at the end of each daysay, a reminder to turn off the coffeepot. Or, suppose you want to make a backup copy of specific files each week. You can do this by using the crontab command to schedule commands or scripts to run regularly at times you specify. In doing so, you can schedule tasks to occur on specific days at specific times and know that the jobs will happen unattended (Figure 9.2).
Figure 9.2. The cron file, which is where you specify the cron job, opens in your default editor. If you've previously specified cron jobs, they'll show up in the editor.
To schedule a regularly occurring job with cron:
At the shell prompt, type crontab, followed by the -e flag, which lets you edit your cron file. As shown in Figure 9.2, your cron file will appear in your default editor. It's likely to be empty (if you haven't set up cron jobs before), but you might have some content in there.
55 16 * * 1-5 mail -s "Go home now!" firstname.lastname@example.org
On the first line of the cron file, enter values for minutes, hours, day of the month, month, and day of the week, then the command you want to run. See the What Are Those Funky Numbers? sidebar for more details about specifying times and days. In this example, we're sending an email to ejray every weekday at 4:55 p.m. reminding him to go home.
Save and close the file.
Chapter 4 will give you a quick reminder about saving and closing with pico and vi.
If you set the times and dates correctly (that is, if you didn't accidentally set them to happen in the 59th hour of the day or whatever), you'll see a message like the one near the end of Code Listing 9.4, confirming that you're all set. (You'll get an appropriate error message if you scheduled something to happen at 55 hours, 12 minutes, on the ninth day of the week.)
Code Listing 9.4. This job reminds ejray to go home every day. The message toward the end indicates that the cron job has been successfully entered.
55 16 * * * mail -s "Go Home Now!" email@example.com < /dev/null ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "/tmp/crontab.16206" 3 lines, 192 characters written crontab: installing new crontab [ejr@hobbes ejr]$
When scheduling cron jobs, you need to specify full and absolute paths to the filesthat is, specify /home/ejray/file rather than file. Also, if you write a shell script and reference it in a cron job, you'll need to specify paths in the shell script as well. cron doesn't check out your personal environment variable settings when it runs, so the full path name is essential.
Use crontab -l to display a listing of your cron jobs.
What Are Those Funky Numbers?
When entering a cron job, you specify
Day of the month (131)
Days of the week (06, with Sunday as 0)
If you replace the number with a *, cron will match all possible values, so, if a job is scheduled for
1 * * * *, it will happen at one minute after every hour
15 3 * * *, it will happen at 3:15 a.m. every day
59 23 31 * *, it will happen at 11:59 p.m., seven times a year (once in each of the months with a 31st)
0 12 * * 0, it will happen at noon on Sundays
You can use a comma to separate multiple values. For example, if you want something to happen on the hour and half-hour during December, you might use 0, 30 * * 12 *.
Use a hyphen (-) to indicate a range. For example, to schedule something for every hour from 9 a.m. to 5 p.m. every day, use 0 9-17 * * *.