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 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.

Figure 9.1. To schedule 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:

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:

  • at noon tomorrow

  • at 01/01/99

  • at 3:42am

  • at now + 3 weeks

  • at teatime

Yes, teatime is a valid option. It's at 4 P.M., by the way.

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.

Code Listing 9.1. 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> 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.
TaDaaaa! Use this key combination to finish the sequence (Code Listing 9.1).

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.


Code Listing 9.2. Delete scheduled jobs by specifying the job number.
[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:

  • Minutes (0–59)

  • Hours (0–23)

  • Day of the of month (1–31)

  • Month (1–12)

  • Days of the week (0–6, with Sunday as 0)

If you replace the number with a *, crontab 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., 7 times a year (once in each of the months with a 31st)

  • 0 12 * * 0, it will happen at noon on Sundays

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 *.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.189.23