Schedule Jobs in Ubuntu (and Other Major GNU/Linux distros)

In Ubuntu, how do you schedule some jobs (shell commands) to be executed in the future? You can use at.

Add a Scheduled Job

Run a terminal emulator. Change to the directory where you want to run the command, and issue:

$ at time_specification

The time specification can be rather sophisticated. You can specify HH:MM (e.g., 15:30), midnight, noon, teatime (4pm), month-day day (e.g., Dec 15), time + days (e.g., 4pm + 3 days, to run a job at 4pm three days from now), time + date (e.g., 10am tomorrow), etc. Don't worry that it will misunderstand your schedule as you can (and should) verify it later.

Now, type the commands you want to schedule. For example, to check who are still awake 3am tomorrow, you can do

$ at 3am tomorrow
warning: commands will be executed using /bin/sh
at> who > why_are_you_still_awake.log
at> ^D<EOT>
job 47 at Sun Dec  5 03:00:00 2010
$

Note that you need to press Ctrl+D to generate <EOT> to tell at that you have finished entering all commands. It will then schedule the task and then display the job ID along with the time scheduled. You should double check if the time is what you want.

Show Scheduled Jobs

Use atq to check all jobs scheduled:

$ atq
47	Sun Dec  5 03:00:00 2010 a vyvy
$

Use at -c job to display the actual commands scheduled:

$ at -c 47
#!/bin/sh
# atrun uid=1000 gid=1000
# mail vyvy 0
umask 22
...
(omitted)
...
XAUTHORITY=/var/run/gdm/auth-for-vyvy-sW7p4O/database; export XAUTHORITY
COLORTERM=gnome-terminal; export COLORTERM
cd /home/vyvy || {
	 echo 'Execution directory inaccessible' >&2
	 exit 1
}
who > why_are_you_still_awake.log
$

Besides the job you want it to schedule, you can see that at is smart enough to include extra commands to make sure that your job can be executed properly. They include specifying the shell to be used, specifying your UID and GID, setting environment variables, changing directory to where you want to issue the commands, etc.

Delete Scheduled Jobs

To delete scheduled jobs, just list them out to atrm:

$ atrm job [job...]

Miscellaneous

To schedule to run some jobs whenever the system load levels permit, use batch. Execution of such jobs can be managed by atd. Check their man pages for details.

There is a GUI program, gnome-schedule, that can help you to manage the jobs easier. However, it seems to suffer from some bugs that can delete my existing scheduled jobs sometimes! Tell me if you have been using it without any problem. The one I tried is version 2.1.1, on Ubuntu 10.10.