Monday, September 15, 2008

One-time scheduling of tasks with 'at'

cheduling services with cron and similar tools is great for repetitive tasks but inconvenient for one-time jobs that only need to be accomplished once. For those tasks, use the 'at' command.

At will execute a command at a particular time and is extremely useful for one-time jobs. For instance, if you are running a script or program and want to receive an e-mail when it's done, or need to schedule a job or task for a particular time or date, at will take care of it for you.

For instance, if you wanted to use at to schedule a job to run at 10:00 p.m., you would use:

$ at 10pm -f script

At uses a very useful time specification that will permit you great flexibility with scheduling. To schedule a job for 2:00 a.m. tomorrow, use:

$ at 2am tomorrow -f script

It can also schedule jobs relational to the current time. For instance, to run a job in exactly three hours, use:

$ at now + 3 hours -f script

When scheduling a command with at, it will print out what the job number is and when it will be executed. To view currently scheduled jobs, use the atq command:

$ atq
154    2008-08-04 20:31 a vdanen

To delete a currently scheduled job, use atrm and provide it the job number:

$ atrm 154

Commands can also be provided to at using standard input, or they can be specified by using the -f option, which takes the script or command to run as the argument.

Another way of scheduling jobs with at can be done using the batch command. This will execute a command when the system load average drops below a certain value, usually 0.8 by default. The batch command takes most of the same arguments as at (less a time specification), and is useful for telling the system to execute a new command after currently running processes--that are using large amounts of processing power--are complete.

Finally, at can be restricted to specific users by using the /etc/at.allow and /etc/at.deny files. If at.allow exists, only users listed in the file may use it. If at.deny exists, but at.allow does not, then all users may use at other than those listed in at.deny. By default, any user may use at.

The at command probably isn't something that will be used on a daily basis, but it is useful when certain tasks need to be executed at certain times, regardless of whether you are in front of the computer or not. If nothing else, it makes a great poor man's TO-DO reminder in that you can use it to send an e-mail or SMS message at specific times, as a reminder for certain events.