April 21, 2010

Scheduling Jobs without Cron: Using at or batch command

Cron is one of the most widely used job schedulers in the *nix systems such as Linux. However one needs to add jobs in the crontab file to schedule the jobs and this is not possible (typically) for users without super user privileges and not having privileges of being added in cron.allow. For such users job scheduling is possible with the help of the batch/at command or (batch command). An example for using the at command to schedule the execution of the file present at /tmp/test.sh is as follows:

For example I am using program that touches a file in a given directory. So I create the /tmp/test.sh file by executing the following commands
#> echo "touch /tmp/abc.text" > /tmp/test.sh
#> chmod +x /tmp/test.sh

To use at, first make sure that the atd deamon is running, which is the daemon that is responsible for scheduling the jobs.

#> ps -ef | grep atd

Now schedule the job test.sh at say 03:00 tomorrow

#> at -f /tmp/test.sh 03:00 tomorrow

The output will be something like

job 1 at 2009-11-05 03:00

The process will execute /tmp/test.sh at the given time. Do a 'man at' for a glimpse of the complex time specifications such as tomorrow, or +3 days, which are possible with the at command. Some of the examples are as follows:

#> at -f /tmp/test.sh 03:00
#> at -f /tmp/test.sh 4pm + 3 days
#> at -f /tmp/test.sh 12:00 july 15

The list of jobs can be viewed using the atq command

#> atq
6 2009-11-05 02:00 a username

5 2009-11-06 04:00 a username

Removing the 5th job from the above queue is simple. Execute

#> atrm 5

#> atq
6 2009-11-05 03:00 a username

Hope this is useful as I was not able to find a simple tutorial for the 'at' command. I guess its difficult to google search 'at' :).


Tejaswini Shenoy said...

Ashwin, buddy where were you an year ago? I was looking for something like this then... Does this work in a solaris environ? Any idea?

Mujhe dhoondh dhoondhke cron mila and it just wouldn't work on solaris :(

Ashwin Rao said...

I have no idea about solaris environment, but this should work considering it to be a *nix system.