Sunday 17 March 2013

Scheduler using linux crontab

Suppose if you want to do some scheduler kind of tasks in ubuntu/linux platforms, crontab utility is very useful for that. Here i am going to explain you how to use this crontab for scheduler stuff.

* How to view what are the cron tasks available for you, just do the following command in your terminal/console.

madhan@ubuntu:~$ crontab -l (It will list of what are the scheduler tasks available for current user)

output of the above command is just like this

madhan@ubuntu:~$ crontab -l
*/5 12 17 03 0 bash /home/madhan/my_first_linux_shell_script > /tmp/mdn_cron1.log

detail about the above crontab command

*/5  ---> every 5 mins
12   ---> 12pm
17   --->  Day of the month
03   --->  Month identifier
0     --->  Day of the week( Normally 0(sunday)-6(saturday) )

"bash /home/madhan/my_first_linux_shell_script" ---> (command going to execute on exact time what we mentioned above. "my_first_linux_shell_script" is shell script file which has real commands going to run on scheduled time.)

>  /tmp/mdn_cron1.log (means output redirection to that log file, if anything fails we can just look into that file for further investigation ).

Ok Let's see How to add your tasks into crontab.

1) Let's create a shell script (I am ruby on rails developer, so i am just writing one small shell script which will run one rake task according to my project, so my shell script contains commands related RoR).

#! bin/bash
# This is my first linux shell script
echo "WoW shell script is working fine"
cd /home/madhan/abl/CyncAbl-R3
bundle exec rake cync:load_dilution_history

Just save this file in the extension of ".sh"(But recent ubuntu distributions its not required to save the file with extension of ".sh". So i saved the file without .sh extension)

2) Lets create one text file for your entire cron items.
  
*/5 12 17 03 0 bash /home/madhan/my_first_linux_shell_script > /tmp/mdn_cron1.log

<cron item 2>(as your wish if you have more that one scheduler)
<cron item 3>(as your wish if you have more that one scheduler)
<cron item4>(as your wish if you have more that one scheduler)

Just save the file with the extension of just ".txt". I save this file "my_cron_tab.txt"

3) Let's add that txt file into cron tab utility. The following command will use

 crontab /home/madhan/my_cron_tab.txt

You are almost done!!!

Lets see whether it will display correct cron item or not using this command

crontab -l (Will list out all scheduler task items available in "my_cron_tab.txt" file)

How to edit crontab, just do the following command
crontab -e

NOTE: if you stuck with executing the scheduler crontab, always you can view the log file in  /var/log/syslog. It has some entries, from there you can investigate further. In short just type it in your console "tail /var/log/syslog" it will show few last lines on that file).

Hopefully you can get little bit clear picture about linux crontab stuff.

1 comment :

  1. This efficient scheduling method optimizes resource usage, Way Play Games enabling smoother system management and freeing up time for more critical tasks in the IT landscape.

    ReplyDelete