Thursday, May 7, 2015

CronTab in unix


Put a shell script in one of these folders: /etc/cron.daily, /etc/cron.hourly, /etc/cron.monthly or /etc/cron.weekly

If these are not enough for you you can add more specific tasks eg. twice a month or every 5 minutes or... go to the terminal and type:


crontab -e
this will open your personal crontab (cron configuration file), the first line in that file explains it all (don't you think)! In every line you can define one command to run, and the format is quite simple when you get the hang of it. So the structure is:


minute hour day-of-month month day-of-week command
 
For all the numbers you can use lists eg, 5,34,55 in the first field will mean run at 5 past 34 past and 55 past what ever hour is defined.

You can also use intervals, they are defined like this: */20 this example means every 20th and if in the minutes column this will be equivalent to 0,20,40
So to run a command every monday at 5:30 in afternoon:



30 17 * * 1 /path/to/command
 
or every 15 minutes

 
*/15 * * * * /path/to/command
 
Note that the day-of-week goes from 0-6 where 0 is sunday.


/////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
CronTab real example

it execute script every 1 min
[root@sysadmin Desktop]# crontab -u root -e

*/1 * * * *  /bin/cp /root/Documents/dev.pem /root/Desktop/


it copy file from document to desktop



//////////////////////////////////////////////////////////////////////////////////

Alternate Hour or 3 Hourly Schedule

If you want something to run once every two hours, you will have to use the slash, "/", character in your field. The slash character is the "step" character. In the case of a two hourly schedule, your time component of your cron file will read:
0 */2 * * *
The second field, "*/2", means every alternate hour.
Similarly, if you want something to run every 3 hours, you can change that field to "*/3", and so on.

No comments:

Post a Comment