Occasionally, you may need to schedule jobs to run one time, at a time you designate. For example, you could schedule an e-mail message to yourself, reminding you to attend a staff meeting. Or, you could schedule a meeting reminder for your coworkers that includes a meeting agenda. You can schedule these and other one-time jobs using at, which lets you designate a time at which a job (or jobs) should run. Figure 9.1 demonstrates scheduling an e-mail about that all-important staff meeting.
To schedule a one-time job with at:
1. | at 12:01 1 Jan 2000 To begin, specify when you want the job to run, using at plus a time statement (Figure 9.1). In this example, we specify a time, month, date, and year, although you can create a variety of other time statements, like these:
|
2. | mail -s "Staff Meeting at 8:30am" → ejr < ~/agenda Specify the job. In this case, it sends e-mail to the user (ejr), specifies the subject "Staff Meeting at 8:30am" and sends the contents of the file called agenda. See Chapter 11 for the full scoop on using mail. |
3. |
Indicate that you've finished issuing commands. |
[ejr@hobbes ejr]$ at midnight at> tar -icf ~/bigdog.tar ~/HereKittyKitty at> gzip ~/bigdog.tar at> uuencode ~/bigdog.tar.gz bigdog.tar.gz | mail -s "Read this by lunch time" deb at> at> EOT> warning: commands will be executed using /bin/sh job 12 at 1998-08-28 00:00 [ejr@hobbes ejr]$ |
To schedule sequential one-time jobs with at:
1. | at midnight Specify when you want the sequential jobs to run, using at plus a time statement. You can use a variety of time statements, as shown in the previous example. |
2. | tar -icf ~/bigdog.tar → ~/HereKittyKitty Enter the first job you want to run. This job collects all of the files from the directory called ~/HereKittyKitty into a single file called ~/bigdog.tar. Chapter 13 will tell you more about archiving with tar. |
3. | gzip ~/bigdog.tar Enter the next job to run. This compresses the ~/bigdog.tar file, making it easier to store and e-mail. |
4. | uuencode ~/bigdog.tar.gz → bigdog.tar.gz | mail -s → "Read this by lunch time!" deb Specify the next job in the sequence. Here, we've uuencoded the compressed file and specified a name for it (uuencode ~/bigdog.tar.gz bigdog.tar.gz), and we've piped the uuencoded file to the mail command (mail -s "Read this by lunch time!" deb). See Chapter 12 for more on mailing, and 13 for more on uuencoding. |
5. |
To delete a scheduled job:
1. | atq For starters, show the list of jobs waitingin the at queue with atq (Code Listing 9.2). The second column, which shows the scheduled time, should jog your memory about which job is which. The first column, which specifies the job number for each job, lets you identify which job to delete in the next step. |
2. | atrm 3 Remove the queued job by typing in atrm and the job number—in this case, job number 3. |
Tip
atq is also handy for reviewing jobs that you've scheduled.
Tip
If you have a long list of commands that you want to run periodically, consider making them into a brief shell script, then using at to run the shell script. It's less work in the long run, and you don't have to concentrate on getting the commands just right as you do when telling at what to do. See Chapter 10 for the full scoop on shell scripts.
[ejr@hobbes ejr]$ atq 4 1998-08-28 12:01 a 9 2000-01-01 12:01 a 13 1998-08-27 16:00 a 12 1998-08-28 00:00 a [ejr@hobbes ejr]$ atrm 12 [ejr@hobbes ejr]$ atq 4 1998-08-28 12:01 a 9 2000-01-01 12:01 a 13 1998-08-27 16:00 a [ejr@hobbes ejr]$ |
What are those funky numbers?When entering a crontab job, you specify:
If you replace the number with a *, crontab will match all possible values, so, if a job is scheduled for
Additionally, you can use a comma to separate multiple values. For example, if you want something to happen on the hour and half-hour throughout December, you might use 0,30 * * 12 *. |
18.191.189.23