Scheduling One-Time Jobs with at
Occasionally, you may need to schedule jobs to run one time, at a time you designate. For example, you could schedule an email message to yourself, reminding you to attend a staff meeting. Or, you could schedule a meeting reminder for your co-workers 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 email about that all-important staff meeting.
Figure 9.1. To schedules a one-time job, all you have to do is specify the time and the job to run.
To schedule a one-time job with at:
at 12:01 1 Jan 2007
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
mail -s "Staff Meeting at 8:30am" ejr < ~/agenda
Specify the job. In this case, it sends email 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.
Indicate that you've finished issuing commands.
To schedule sequential one-time jobs with at:
Specify when you want the sequential jobs to run, using at plus a time statement (Code Listing 9.2). You can use a variety of time statements, as shown in the previous example.
Code Listing 9.2. To schedule sequential one-time jobs, just specify the time and the jobs in the order you want them to run.
[ejr@hobbes ejr]$ at midnight at> tar -icf ~/bigdog.tar ~/HereKittyKitty at> gzip ~/bigdog.tar at> mutt a bigdog.tar.gz -s "Read this by lunch time deb < /dev/null at> at> <EOT> warning: commands will be executed using /bin/sh job 12 at 2003-08-28 00:00 [ejr@hobbes ejr]$
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.
Enter the next job to run. This compresses the ~/bigdog.tar file, making it easier to store and email.
mutt a bigdog.tar.gz -s "Read this by lunch time!" deb < /dev/null
Specify the next job in the sequence. Here, we're using mutt's command-line mail options to attach a file, specify a subject, and mail the whole shebang to Deb. See Chapter 11 for more on emailing with mutt.
Ta-daaaa! Use this key combination to finish the sequence.
To delete a scheduled job:
For starters, show the list of jobs waiting in the at queue with atq (Code Listing 9.3). 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.
Code Listing 9.3. Delete scheduled jobs by specifying the job number.
[ejr@hobbes ejr]$ atq 4 2003-08-28 12:01 a 9 2004-01-01 12:01 a 13 2003-08-27 16:00 a 12 2003-08-28 00:00 a [ejr@hobbes ejr]$ atrm 12 [ejr@hobbes ejr]$ atq 4 2003-08-28 12:01 a 9 2004-01-01 12:01 a 13 2003-08-27 16:00 a [ejr@hobbes ejr]$
Remove the queued job by typing atrm and the job numberin this case, job number 12.
atq is also handy for reviewing jobs that you've scheduled.
Use at to send yourself reminders.
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.
Different flavors of Unix sometimes present the information from at differently. You get all the information you need, but it may be arranged somewhat differently.