Cron in Linux
What is Cron
Crontab in linux is use to schedule the program or scripts to run at specific time or run at specific intervels.
How to setup cron
To schedule the job we need to setup the crontab. There are 2 parts in crontab Time of running script/programm and name of program or script
1 Time of Running Cron (5 parameter separated by space)
<Minute> <Hour> <DayOfMonth> <Month> <WeekDay>
WeekDay 0-6 (0 is sunday)
You can use below values to setup the cron
* any value
, value list separator
– range of values
/ stpe value
If you want to run at 4:08 AM daily
08 04 * * *
If you want to run on every sunday 11 PM then
* 11 * * 0
path of script to rung
Example if you want to run script to run every min you need to put 5 Stars
* * * * * <Script to Run>
There is command crontab -e to modify the cron however I use the file. You can write the contab in file and give command
It will setup the cron as mentioned in file.
Example we need to run script PerformanceMonitor.sh which is in directory /opt/monitor directory, should run at every min
We will create file
vi myCron * * * * * /opt/monitor/PerformanceMonitor.sh Save file crontab myCron
This will setup the cron
to view the currently cron configured we can use crontab -l command.
How to debug if cron is not working
many times it happens that we expect output from cron script but we do not get, there is something is wrong (due to envirounment veriable or some other issue).
If we do not redirect the output of the cron then the output goes to mail so if you login and see message that you have mail in unix then check with the mail command. you will get the error details.
or else you can redirect the script to error log file and check the error message (Check http://learnntry.com/standard-input-output-and-error/ for this),
if it says that file not found then you may have given script name solution is you can use asulate path.
if it says permission then check the script permission or output
some times env variable not set then you need to source the profile file in start of your script.