Thursday, August 28, 2008

Details about PHP Cron Job with examples

Introduction to Cron

* There are lots of things to automate in Unix
* Many of them are done routinely
* You (the administrator) have better things to do
* Cron (and some appropriate shell scripts) can do them for you


Cron


This file is an introduction to cron, it covers the basics of what cron does,
and how to use it.

What is Cron?


Cron is the name of program that enables unix users to execute commands or
scripts (groups of commands) automatically at a specified time/date. It is
normally used for sys admin commands, like makewhatis, which builds a
search database for the man -k command, or for running a backup script,
but can be used for anything. A common use for it today is connecting to
the internet and downloading your email.

This file will look at Vixie Cron, a version of cron authored by Paul Vixie.

How to start Cron


Cron is a daemon, which means that it only needs to be started once, and will
lay dormant until it is required. A Web server is a daemon, it stays dormant
until it gets asked for a web page. The cron daemon, or crond, stays dormant
until a time specified in one of the config files, or crontabs.


PHP Cron jobs with Examples


The format of an cron tab entry is:

* * * * * command_to_be_executed

| | | | |

| | | | |_ day of the week (0-6)

| | | |__ month(1-12)

| | |____ day of the month(1-31)

| |______ hour(0-23)

|________ minute(0-59)


Now, take an interest in some special characters (metacharacters) :
- *, if one of the m h dom mon dow fields owns the * character, then it indicates evey minute or evey hour or every day or every day of the month or every month or every day of the week, it depends on which field is placed *.
- / permits to specify a repetition.
- - permits to define a range.
- , permits to specify several values.


Some samples:


*/5 * * * * command to execute a command every 5 minutes.

0 22 * * 1-5 command to execute a command every day, monday to friday, at 10 p.m.

17 19 1,15 * * command means the first and the fifteenth day of the month at 19h17 (7.17 p.m.)

23 0-16/2 * * * command means every 2 hours at the twenty-third minute, between midnight and 16h00 (4.00 p.m.)



Example 1 :


If you want a cron job to run on every day 10 a.m. then the cron command will be like,

0 10 * * * wget -O /dev/null http://manoilayans.com/expired_users.php

here the value 10 is given on the 2nd position, because 2nd position is for hour in the command syntax.

Example 2 :


If you want a cron job to run on every month first day (ex: 01-10-2008) at 5 a.m. then the cron command will be like,

0 5 1 * * wget -O /dev/null http://manoilayans.com/expired_users.php

here the value 5 is given on the 2nd position, because 2nd position is for hour (5 a.m.) and the value
1 is given in the position 3rd position, because 3rd position is for 'day of the month' in the command syntax.


Example 3 :


The following command should run by cron at 1 am on everyday.

0 1 * * * wget -O /dev/null http://manoilayans.com/expired_users.php


Alternative way to run Cron job:


Example 1:

If you want a cron job to run on every day 10 a.m. then the cron command will be like,

[root@mylinux ~]#crontab -e 0 10 * * * /usr/bin/php -q /www/htdocs/phpdocs/expired_users.php

here,
'/usr/local/bin/php' - where php is installed in our server
'-q' - run the file
'/www/htdocs/phpdocs/expired_users.php' - path for the executable file

Example 2:

If you want a cron job to run on every month first day (ex: 01-10-2008) at 5 a.m. then the cron command will be like,

[root@mylinux ~]#crontab -e 0 5 1 * * /usr/bin/php -q /www/htdocs/phpdocs/expired_users.php


Example 3:

The following command should run by cron at 1 am on everyday.

[root@mylinux ~]#crontab -e 0 1 * * * /usr/bin/php -q /www/htdocs/phpdocs/expired_users.php


Example 4:

The following command should run by cron at every 5 minutes.

[root@mylinux ~]#crontab -e */5 * * * * /usr/bin/php -q /www/htdocs/phpdocs/expired_users.php

Conclusion:
* There are lots of things to automate in Unix
* Many of them are done routinely
* You (the administrator) have better things to do
* Cron (and some appropriate shell scripts) can do them for you

No comments:

Popular Posts