May 29, 2006

CLI Magic: Executing jobs once

Author: Matthias Kalle Dalheimer & Matt Welsh

You know you can schedule recurring jobs with the cron command. What if you want to run a certain command just once, or a limited number of times, but still at times when it is inconvenient to type in the command interactively? You could always add the command to the crontab and then remove it later. But there is also a tool that is made for this job: the at command.

User Level: Intermediate

This article is excerpted from the recently published book Running Linux, 5th Edition, copyright © 2006, 2002, 1999, 1996, 1995 O'Reilly Media, Inc. All rights reserved.

at reads commands to be executed from a file or from standard input. You can specify the time in a number of ways, including natural-language specifications such as noon, midnight, or, interestingly, teatime (which, much to the dismay of British users, maps to 4 p.m.).

For at to work, the at daemon, atd, needs to run. How it is started depends on your distribution: rcatd start and /etc/init.d/atd start are good tries. In a pinch, you should also be able to just run /usr/sbin/atd as root.

As an example, let's say that you want to download a large file from the Internet at midnight when your ISP is cheaper or when you expect the lines to be less congested so that the probability of success is higher. Let's further assume that you need to run a command connectinet for setting up your (dial-up) Internet connection, and disconnectinet for shutting it down. For the actual download in this example, we use the wget command:

    $ at midnight
    warning: commands will be executed using /bin/sh
    at> connectinet
    at> wget ftp://overloadedserver.lotsastuff.com/pub/largefiles/reallylargefile.bz2
    at> disconnectinet
    at> <EOT>
    job 1 at 2005-02-26 00:00

After typing at midnight, the at command first tells us that it is going to execute our commands with another shell (we are using the Z shell for interactive work here, whereas at will be using the Bourne shell) and then lets us enter our commands one after the other. When we are done, we type Ctrl-D, which at shows as <EOT>. at then shows the job number and the exact date and time for the execution. Now you can lean back in confidence that your command will be issued at the specified time -- just don't turn off your computer!

If you are unsure which commands you have in the queue, you can check with the atq command:

    $ atq
    1       2005-02-26 00:00 a kalle

This shows the job number in the first column, then the date of the planned execution, a letter specifying the queue used (here a), and finally the owner of the job.

If you decide that it wasn't such a good idea after all to submit that command, you can cancel a job if you know its job number by using the atrm command:

    $ atrm 1

atrm is one of the more taciturn commands, but you can always use atq to see whether everything is as expected:

    $ atq

Not much talk, either, but your command is gone.

Category:

  • Linux
Click Here!