Creating Jobs to Run One Scheduled Time with the at Command


Creating Jobs to Run One Scheduled Time with the at Command

Okay, so cron is amazingly useful for regularly scheduled tasks. However, what about a task that you only want to happen oncesomething you want to postpone until some later time when you won't be around to execute it yourself? You could do this with cron, setting up a crontab file to have an entry that runs crontab -u at the end to delete itself. However, there is a better way: the at program.

Note

As an example of how useful the at command can be, I used it this past summer during a road trip to Alaska. Each morning an at command, which I had set up in advance, posted an entry to my blog that described where on the Alaska Highway I was going to be that day. I found, however, that it wasn't as necessary as I'd expected because there's Internet access even in the Yukon these days....


The at program is actually made up of several commands: at (the job creator), atq (which displays pending jobs), and atrm (which lets you cancel pending jobs listed in atq). There is also a batch command, which is a version of at that will run only if the system load is less than some value (1.5 is the compiled-in default).

When you use at to create a job, it reads commands, line by line, in /bin/sh script style. These commands can be specified either on the command line (standard input) or with a preexisting file. Either way, you simply enter the at command followed by a timeformatting string, which can take a number of fairly intuitive forms, as shown in Table 15.4 (a full discussion of the time-formatting options is available in man at).

Table 15.4. Syntax Examples of the at Command

Command String

Meaning

at 10pm

Executes at 10:00 p.m. on the current day or the next day if it's after 10:00 p.m.

at 8:00am May 15

Executes at 8:00 a.m. on May 15

at midnight Jan 1 2006

Executes on the first second of the year 2006

at teatime tomorrow

Executes at 4:00 p.m. the following day


After you type this string and press Enter, you are in standard input mode. Type your commands line by line, pressing Enter each time. When you finish entering commands, press Ctrl+D to exit and place the job into the queue. You can alternately create an input file beforehand and then feed it to at:

# at -f mycommands noon + 5 days


This reads a plain-text file called mycommands that contains your commands, like a batch file, and executes them using /bin/sh at noon five days from the time you entered the command.

You can view existing jobs with the atq command:

# atq Date                  Owner   Queue   Job# 23:00:00 03/28/01     root    c       2


You can cancel jobs by ID number with the atrm command:

# atrm 2


Note

The way at jobs are executed involves the atrun command. The atrun tool runs every five minutes (in the standard FreeBSD installation), reads all pending jobs for all users, and executes all jobs whose execution time has passed. You can modify how frequently atrun runs by changing its entry in /etc/crontab, although this should not be necessary unless you need higher resolution than every five minutes.





FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net