Suppose you want to send yourself a reminder message just before you go home at the end of each day—say, 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).
1. | crontab -e 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. | |
2. | 55 16 * * 1-5 mail -s "Go home now!"
→[email protected]
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. | |
3. | 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.
|
✓ Tips
When scheduling cron jobs, you need to specify full and absolute paths to the files—that 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
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 * * *. |
52.14.84.29