Flipkart

Wednesday, June 16, 2010

Run Script From Cron

The cron man page defines cron as a "daemon to execute scheduled commands". This means you can run scripts every hour, or every day, or on the 13 of every month at 5:38am. Here is an example of a cron entry.
00 18 * * * /path/to/command
In a few short minutes you will understand what this command means.

Structure

The structure of a cron entry as seen above is quite simple when broken down. In the above entry there is five sections and a path to a PHP script. Here is how it breaks down
Minute Hour Day Month Day Command
Lets break it down further..
Minute
Minute of the hour, 00 to 59
Hour
Hour of the day in 24-hour format, 00 to 23
Day
Day of the month, 1 to 31
Month
Month of the year, 1 to 12
DayDay of week
3 chars - sun, mon, tue, or numeric (0=sun, 1=mon etc)
Command = The command you want to execute
The time is in twenty four hour time. So you may have guessed already that the 00 is the minutes and the 18 above is 6:00pm. The asterix character * is a wild card and means every. That is, every minute or every day, or every month. This means the script.php script would be executed every day of every month.
00 18 * * * /path/to/script.php

Crontab

The crontab is the command used to interface with cron. Within crontab command can be entered, editted or deleted. To enter into the crontab simply type:
crontab -e
This command will bring you to a vi console where you can create your cron entries. All the same rules that apply to vi apply in the editor. It is important to follow the examples here by using absolute paths to the script that is to be executed. This is because crontab does not have access to the system $PATH, so any system command also need have thier full path also. The path to a system command can be aquired using the which command. The $PATH is not available within cron, however, the $HOME variable is, so that when we need to execute a command or script from our home directory, this is available to us.
#which
mail /bin/mail

Set Crontab Editor

It is hard to imagine, yet there are still people out there who do not use vi(m). On most unix and unix like systems vi(m) is the default editor for crontab. This means when you type "crontab -e" at the command prompt, the crontab file is opened in vi(m). To alter this behaviour, the default editor can be changed or exported with the export command. Shown here is an example of changing the default crontab editor to joe.
export EDITOR=joe

Examples

We saw above how a script could be run at 6:00pm every night. But what is a script needs to be executed every 5 minutes. We could do something like this.
0 ,5,10,15,20,25,30,35,40,45,50,55 * * * * /path/to/command
This example shows how the time increments are seperated by a space, but we can have multiple values seperated by comma's. Of course, this is a lot to type out, so cron has a little shortcut syntax to achieve the same result.
*/5 * * * * /path/to/command
The previous examples show how to run every day or every whatever. But what if we wish to run a PHP script at 4:00am each Friday just before we go home.
00 16 * * fri /usr/local/bin/php $HOME/script.php
The above cron entry will shows 4:00pm every Friday (fri) and will execute the command /usr/local/bin/php $HOME/script.php. Note the use of the $HOME variable to show that the script.php file to be executed is within the users home directory. The path to the php executable however, must be an absolute path. There is more to cron than these simple demonstrations have shown. It is hoped that this is enough to get you up and scheduling tasks with the cron and the crontab. Just remember the sequence of times - Minute Hour Day Month Day Command

Backup MySQL Database With Cron

Database backup are a frequently performed task, usually a webmaster will begin this process after losing their data and realise that this needs to be done on a regular basis. This is an ideal situation where cron can be used. Database backups should be stored by date. That is, the filename of the SQL dump should be .todays_date.sql so a backup can be made from a given time and/or date. to achieve this, enter the following command in crontab
0 2 * * * /usr/local/bin/mysqldump -db_user -db_password db_name > /home/www/backups/dbname-`date +\%s`.sql

No comments:

Post a Comment