Evanescent Thoughts

Evanescent Thoughts

Using “at” command to schedule jobs in Linux

with 11 comments


Everything has a strong motivation behind it and the reason i am writing on this is because it made downloading from torrents easy for me during the off-charge hours. I usually had to stay awake till 2 am in the morning and put it for download and then have to get up again at morning 8 to switch it off.I was too lazy to do something and at came to my rescue.And last month i usually dont get up by 8 in the morning and i used to overshoot my 1.5GB download limit. ๐Ÿ˜ฆ

You can always do the following using cron, but again at has its own speciality. While in case of cron , the job runs at the scheduled frame of time on everyday , at can be used to schedule jobs which dont happen regularly. Like my case, i didnt want to download everyday so had to put the download only whn i am at home ๐Ÿ™‚ .

So at command works like this

sathya@Phoenix:~$ at 11:30AM
warning: commands will be executed using /bin/sh
at> echo "I was executed at 11:30AM" > ~/Desktop/temp
at> <EOT>
job 30 at Sun Jan 18 11:30:00 2009
sathya@Phoenix:~$

All the above command does is ,it prints the line “”I was executed at 11:30AM”ย  at 11:30 AM . Since the command is run with a shell of its own, to see any sort of output displayed to the stdout , i redirect it to a file of my choice (~/Desktop/temp).There are many ways to specify the time for the at command . For a complete list on the date day , time formats chk /usr/share/doc/at-3.1.8/timespec which contains the exact definition of the time specifications.

You can now do a simple cat ~/Desktop/temp to read the output after 11:30 AM ๐Ÿ™‚ .

Now coming to options for at .

Use atq to know the current jobs in queue. Jobs that are running have a status as” =” and jobs to be scheduled have a status “a” .The displayed output is the formatย  <job-id> <Time > <jobstatus> <user>. Each scheduled job has a unique ID usually numbered from 0

sathya@sathya@Phoenix:~$ atq
30ย ย ย  Sun Jan 18 11:30:00 2009 a sathya
sathya@Phoenix:~$ Phoenix:~$

To know the command to be executed corresponding to a job id , use the following option of at -c <job-ID> . This displays all the details for the job including the variables used for the shell to be invoked for this job . And hence wht u see mostlty would be junk except for the last line which has the command to be executed. So use a tail option to read tht one alone. ๐Ÿ™‚

sathya@Phoenix:~$ at -c 30 | tail -2
echo "I was executed at 11:30AM" > ~/Desktop/temp
sathya@Phoenix:~$

To delete a job use atrm <job-ID> .

Now for ex ,
sathya@Phoenix:~$ at 11:58AM
warning: commands will be executed using /bin/sh
at> gnome-terminal
at> <EOT>
job 31 at Sun Jan 18 11:58:00 2009
sathya@Phoenix:~$

The above command as expected is supposed to open a terminal for u at 11:58 . But it doesnt . Because at by defaultย  does only console applications. To make GUI apps , all u need to do is a small modification ( thanks to akhil for pointing this out ).

sathya@Phoenix:~$ at 11:59AM
warning: commands will be executed using /bin/sh
at> DISPLAY=:0 gnome-terminal
at> <EOT>
job 32 at Sun Jan 18 11:59:00 2009
sathya@Phoenix:~$

At command reads usually the commands from stdin but using the -f option allows us to read from a file.All variables except the $DISPLAY and $TERM are retained from the time the command was put into queue. All these variables and their associated values can be viewed using at -c <job-ID>.In case of using non console applications , u need to specify the $DISPLAY manually as shown above .

Ok so the final thing i ll be doing to start my “transmission bit torrent” at 2.00 am and stop it at 8.00 am is the following

sathya@Phoenix:~$ at 2.00AM
warning: commands will be executed using /bin/sh
at> DISPLAY=:0 transmission
at> <EOT>
job 35 at Mon Jan 19 02:00:00 2009
sathya@Phoenix:~$ at 8.00AM
warning: commands will be executed using /bin/sh
at> killall -r transmission
at> <EOT>
job 36 at Mon Jan 19 08:00:00 2009
sathya@Phoenix:~$

This solves the job of taking care of my downloads.And for a GUI version of this, use Ktimer. But u will have to kill yourself if u r gonna use it after having something cool “at” you :P. Any suggestions or improvements or new approaches can be put in comments ๐Ÿ™‚

Advertisements

Written by Sathya Narayanan

January 18, 2009 at 12:17 pm

11 Responses

Subscribe to comments with RSS.

  1. Why do you need a -r for killall ? Is “transmission” a regular expression ๐Ÿ˜› ?

    Akhil Ravidas

    January 18, 2009 at 12:31 pm

    • thts something tht comes out of usual killall -r firefox* in sun lab :P.. cant help ๐Ÿ™‚

      Sathya Narayanan

      January 18, 2009 at 12:35 pm

  2. Hey, nice post man ๐Ÿ™‚ Will be useful for me too ๐Ÿ™‚

    One small doubt. What happens to the commands which needs input in between? Like anyway I can have a sudo command run using at??

    sp2hari

    January 18, 2009 at 12:43 pm

  3. @ hari .. i think u can write a script and take inputs frm file.. using -f option.. wht say ?

    Sathya Narayanan

    January 18, 2009 at 1:10 pm

  4. fascinating and communicative, but would be suffering with something more on this topic?

    Shergiectters

    February 12, 2009 at 12:04 am

  5. 10x ๐Ÿ™‚

    Info put in best way possible

    mahesh

    July 6, 2009 at 10:36 pm

  6. The DISPLAY=:0 bit sorted my particular problem, many thanks Sathya.

    rob

    October 5, 2009 at 6:03 am

  7. Nice tip man….it helps me a lot ๐Ÿ™‚ thanks buddy

    Ahmed

    April 15, 2011 at 12:01 am

  8. Superb explanation.. great work…..(:

    Manoj

    March 30, 2012 at 8:54 am

  9. Hi there, I discovered your web site by the use of Google even as searching for a similar topic,
    your website came up, it seems to be good. I’ve bookmarked it in my google bookmarks.
    Hello there, just became alert to your blog via Google, and located that it is truly informative. I am gonna be careful for brussels. I’ll be
    grateful should you continue this in future. Numerous people can be benefited from your writing.
    Cheers!

    www.crazzee.com

    December 18, 2012 at 5:49 am

  10. Very helpful tip about DISPLAY

    Jini

    December 19, 2012 at 2:20 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: