Saturday, April 6, 2013

Scheduling tasks in Linux using at commad

As a serious Linux user is considered, scheduling tasks is inevitable. In this post lets see how to automate your tasks using at commands.
General syntax is
$ at time date 
After entering at command it gives the user a prompt to enter the commands to be executed one by one. Once over press Ctrl+D.
Enter the following in your terminal
anil@anil-VirtualBox:~$ at 10:20pm
warning: commands will be executed using /bin/sh
at> touch anil.txt
at> <EOT>
job 81 at Sat Apr  6 22:20:00 2013
This will execute the command touch anil.txt at 10:20 am
Instead of giving commands to be executed on stdin, to can list your commands in a file. Use the -f option to the pass the file which contains commands to be executed.
For example lets create a file named cmdlist.txt with the following contents.
touch testout.txt
echo "Learning at commands"  >testout.txt
anil@anil-VirtualBox:~$ cat cmdlist.txt 
touch testout.txt
echo "Learning at commands"  >testout.txt
Now enter the following to schedule
at -f cmdlist.txt 6:18 pm
This will execute the commands in cmdlist.txt at 6:18 pm

Following is a list of examples
at -f cmdlist.txt 6:22 pm april 6 2013
executes at the specified time and date
at -f cmdlist.txt now + 18 minutes
at -f cmdlist.txt now + 18 hours
at -f cmdlist.txt now + 18 days
at -f cmdlist.txt now + 18 months
at -f cmdlist.txt now + 18 years
The above examples uses relative time/date. First one executes after 18 miniutes from now. Rest is self explanatory

An example for performing routine tasks is shown below.
Create a file cmd_routine.txt with the following contents 

echo "Printing the date and time of modification $(date)"  >>/home/anil/testoutput.txt
at -f cmd_routine.txt now + 1 minutes

and now execute the following
at -f cmd_routine.txt now + 1 minutes
This will execute the commands in  cmd_routine.txt every one minute. Here we actually used the idea of recursion i.e. calling the command from within it. 
To verify its operation, every time the command is executed, current system date and time will be printed to the file testoutput.txt as shown below
anil@anil-VirtualBox:~$ cat testoutput.txt
Printing the date and time of modification Sat Apr  6 19:16:00 IST 2013
Printing the date and time of modification Sat Apr  6 19:17:00 IST 2013
Printing the date and time of modification Sat Apr  6 19:18:00 IST 2013
Printing the date and time of modification Sat Apr  6 19:19:00 IST 2013
Printing the date and time of modification Sat Apr  6 19:20:00 IST 2013
Printing the date and time of modification Sat Apr  6 19:21:00 IST 2013
To view all the scheduled jobs use atq command as shown below
anil@anil-VirtualBox:~$ atq
336    Sun Apr  6 18:47:00 2031 a anil
334    Thu Apr 18 18:43:00 2013 a anil
339    Mon May  6 19:01:00 2013 a anil
337    Sun Apr  6 18:47:00 2031 a anil
335    Mon Oct  6 18:44:00 2014 a anil
348    Sat Apr  6 19:24:00 2013 a anil
333    Sun Apr  7 09:41:00 2013 a anil
To remove any scheduled jobs use atrm command as shown below.
anil@anil-VirtualBox:~$ atrm 336
The above one will remove the scheduled job with job number 336

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes