Tuesday, September 9, 2014

How to create a crone job linux

Hi all,


As you do great stuff  with shall script lile backups , processing etc. Most of you are familiar with cron  jobs in linux. But for all those how dont know I am writing this tutorial.

In linux we have a usefull tool name crontab. We can add our cron jobs in crontab .

- To see running jobs in background use
sudo crontab -l
- To add a job
sudo crontab -e

Lets create a script that while same sate of a server in a text file. Let say we have a tomcat server ruining on linux system. And we want to nominator its state in hourly basis

A) Create s script:

1) Create minitor_tomcat.sh  at some location lets say home/<user folder>/
2) Add permission : sudo chmod +x  minitor_tomcat.sh 
3) Open file in gedit
4) Add code
status="$(sudo /etc/init.d/tomcat7 status)"
cdate="$(date)"
echo "$cdate : Tomcat status $status" >> /home/<user folder>/logs.txt;
B) Create a job:

1) call  
2) move cursor to the end of file
3) Follow the below format for creating a job

[MIN] [HR] [DOM] [MON] [DOW] [COMMAND] 

MIN : It is the Minute Field which can take values from 0 to 59.

HR : It is the HOUR Field which can take values from 0 to 23.

DOM : It is the Day of Month Field which can take values from 1 to 31.

MON : It is the Month Field which can take values from 1 to 12.

DOW : It is the Day of Week Field which can take values from 0 to 7, where 0 and 7 are Sundays.

So to run file hourly
* */1 * * * bash /home/<user folder>/minitor_tomcat.sh

After writing save  file, You can see your cron by using command sudo crontab -l

For more details about timing follow this.


You can run any command. I mean not only bash file but any command using crontab

For example if I wana write some text in a file monday I can write

* * * * 1 echo "hello job" >> /home/<user folder>/file.txt