Nov 15, 2007

I learned about "Cron"

Cron is a time based scheduling daemon in Linux ( not only in Linux anyway :D ) when I installed Linux, cron was already installed and ready to be used. The schedule run by cron not only single command but also can be expanded to many commands in a single script. So, at a time you just set cron to execute the script which consisted many commands.


For Example : I want to shutdown the internet connection every sunday and turn it on again on monday morning.....so in crontab ( cron configuration file, can be found in /etc ), I will put two cron syntaxes which turn off and turn on the network at that desired time.


For more info about cron :-->
http://en.wikipedia.org/wiki/Cron
http://www.unixgeeks.org/security/newbie/unix/cron-1.html
http://www.ussg.iu.edu/usail/automation/cron.html

Basic Syntax of cron :--> ( from wikipedia )

+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +--- day of week (0-6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed


each * can be replaced with my desired values, and not only with a single value
for example, if I want to do something every ten minutes ( ignoring the hour, DOM, month, DOW; means it will execute every ten minutes as long as the machine running ) so the syntax will be like this
10,20,30,40,50,0 * * * * command to be execute

If i want to do something every day at 23:59PM, the syntax will be like
59 23 * * 0,1,2,3,4,5,6 command to be executed or
59 23 * * 0-6 command to be executed

This syntax must be put in /etc/crontab

More examples :
I want to shutdown the internet every whole Sunday and The internet will back on Monday; so, I will add these lines to the server crontab
0 0 * * sun root services network stop
0 0 * * mon root service network restart

0 and 0 is for 00:00, two * mean any DOM and any month, sun and mon were the day that the commands will be executed

I want to shutdown the server on 24 Dec at 23:59 PM and the server will manually turn on after holiday
59 23 24 12 * root shutdown -h now

I want to run something at 3:10 am every day :-->
10 3 * * * root command

I want to run something at 12:10 on day 5 of each month :-->
10 0 5 * * root command

I want to do backup every new year :-->
1 0 * 1 * root backup.sh

~E~

No comments: