Using “at” command to schedule jobs in Linux
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:30AMwarning: commands will be executed using /bin/shat> echo "I was executed at 11:30AM" > ~/Desktop/tempat> <EOT>job 30 at Sun Jan 18 11:30:00 2009sathya@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
. Any suggestions or improvements or new approaches can be put in comments
Why do you need a -r for killall ? Is “transmission” a regular expression
?
thts something tht comes out of usual killall -r firefox* in sun lab
.. cant help
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??
@ hari .. i think u can write a script and take inputs frm file.. using -f option.. wht say ?
fascinating and communicative, but would be suffering with something more on this topic?
10x
Info put in best way possible
The DISPLAY=:0 bit sorted my particular problem, many thanks Sathya.