Using at and batch to Schedule Tasks for Later

If there is a time-intensive task you want to run, but you do not want to do it while you are still logged in, you can tell Ubuntu to run it later with the at command. To use at, you need to tell it the time at which you want to run and then press Enter. You will then see a new prompt that starts with at>, and everything you type there until you press ctrl+D will be the commands you want at to run.

When the designated time arrives, at will perform each action individually and in order, which means later commands can rely on the results of earlier commands. In this next example, run at just after 5 p.m., at is used to download and extract the latest Linux kernel at a time when the network should be quiet:

[[email protected] ~]$ at now + 7 hours at> wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.10.tar.bz2 at> tar xvfjp linux-2.6.10.tar.bz2 at> <EOT>

job 2 at 2005-01-09 17:01

Specifying now + 7 hours as the time does what you would expect: at was run at 5 p.m., so the command will run just after midnight that night. When your job has finished, at will send you an email with a full log of your job's output; type mail at the console to bring up your mailbox and then press the relevant number to read at's mail.

If you have a more complex job, you can use the -f parameter to have at read its commands from a file, like this:

echo wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.10.tar.bz2\; tar xvfjp linux-2.6.10.tar.bz2 > myjob.job at f myjob.job tomorrow

As you can see, at is flexible about the time format it takes; you can specify it in three ways:

• Using the now parameter, you can specify how many minutes, hours, days, or weeks relative to the current timefor example, now + 4 weeks would run the command one month from today.

• You can also specify several special times, including tomorrow, midnight, noon, or teatime (4 p.m.). If you do not specify a time with tomorrow, your job is set for precisely 24 hours from the current time.

• You can specify an exact date and time using hh:mm mm/dd/yy formatfor example, 16:40 22/12/05 for 4:40 p.m. on the 22nd of December 2005.

When your job is submitted, at will report the job number, date, and time that the job will be executed; the queue identifier; plus the job owner (you). It will also capture all your environment variables and store them along with the job so that, when your job runs, it can restore the variables, preserving your execution environment.

The job number and job queue identifier are both important. When you schedule a job using at, it is placed into queue "a" by default, which means it runs at your specified time and takes up a normal amount of resources.

There is an alternative command, batch, which is really just a shell script that calls at with a few extra options. These options (-q b m now, if you were interested) set at to run on queue b (-q b), mailing the user on completion (-m), and running immediately (now). The queue part is what is important: Jobs scheduled on queue b will only be executed when system load falls below 0.8that is, when the system is not running at full load. Furthermore, it will run with a lower niceness, meaning a queue jobs usually have a niceness of 2, whereas b queue jobs have a niceness of 4.

Because batch always specifies now as its time, you need not specify your own time; it will simply run as soon as the system is quiet. Having a default niceness of 4 means that batched commands will get less system resources than a queue job's (at's default) and less system resources than most other programs. You can optionally specify other queues using at. Queue c runs at niceness 6, queue d runs at niceness 8, and so on. However, it is important to note that the system load is only checked before the command is run. If the load is lower than 0.8, your batch job will be run. If the system load subsequently rises beyond 0.8, your batch job will continue to run, albeit in the background, thanks to its niceness value.

When you submit a job for execution, you will also be returned a job number. If you forget this or just want to see a list of other jobs you have scheduled to run later, use the atq command with no parameters. If you run this as a normal user, it will print only your jobs; running it as a superuser will print everyone's jobs. The output is in the same format as when you submit a job, so you get the ID number, execution time, queue ID, and owner of each job.

If you want to delete a job, use the atrm command followed by the ID number of the job you want to delete. This next example shows atq and atrm being used to list jobs and delete one:

14 2005-01-20 23:33 a paul

16 2005-02-03 22:34 a paul

17 2005-01-25 22:34 a paul

15 2005-01-22 04:34 a paul

18 2005-01-22 01:35 b paul [[email protected] ~]$ atrm 16 [[email protected] ~]$ atq

14 2005-01-20 23:33 a paul

17 2005-01-25 22:34 a paul

15 2005-01-22 04:34 a paul

18 2005-01-22 01:35 b paul

In that example, job 16 is deleted using atrm, and so it does not show up in the second call to atq.

The default configuration for at and batch is to allow everyone to use it, which is not always the desired behavior. Access is controlled through two files: /etc/at.ailow and /etc/at.deny. By default, at.deny exists but is empty, which allows everyone to use at and batch. You can enter usernames into at.deny, one per line, to stop those users scheduling jobs.

Alternatively, you can use the at.aiiow file; this does not exist by default. If you have a blank at.aiiow file, no one except root is allowed to schedule jobs. As with at.deny, you can add usernames to at.aiiow one per line, and those users will be able to schedule jobs. You should use either at.deny or at.aiiow: When someone tries to run at or batch, Ubuntu checks for her username in at.aiiow. If it is in there, or if at.aiiow does not exist, Ubuntu checks for her username in at.deny. If her username is in at.deny or at.deny does not exist, she is not allowed to schedule jobs.

Was this article helpful?

0 0

Post a comment