Special Topics - Running Cron Jobs


General Info

Some web applications may require that certain actions are automatically executed by predefined schedule. In UNIX-like operating systems such actions are managed by the cron daemon.

Periodically executed actions are described by means of special instructions added to crontab configuration files. Each instruction must contain a specially formatted description and the command to be executed as shown below:
* * * * * executed command
- - - - -
| | | | |
| | | | ----- Day of the week (0 - 7) (Important: Sunday = 0 or 7)
| | | ------- Month (1 - 12)
| | --------- Date (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Examples of cron job descriptions:
# Executed every 5 minutes
*/5 * * * * $HOME/bin/every5min
    
# Executed every day at 06:30
30 6 * * * $HOME/bin/daily

# Executed each hour on working days
0 * * * 1-5 $HOME/bin/hourly

# Executed at 01:00 from Saturday to Sunday
0 1 * * 7 $HOME/bin/weekly

# Executed at 01:00 every 1st and 15th day of each month
0 1 1,15 * * $HOME/bin/montly


To make sure your application performs such operations you have to create firstly a CronController (you nay give any name you want), where you write executable code. Then you have to set up a cron job in your Hosting Control Panel.

Setting up a cron job in Site5.com Control Panel

ApPHP MVC Framework - cron example

Setting up a cron job in cPanel

ApPHP MVC Framework - cron CPanel

Two options to perform cron jobs

You may define batch or non-batch options to perform the cron jobs. The recommended way to run cron code is to set up a cronjob if you run a Unix/Linux server. If for any reason you can't run a cronjob on your server, you can choose the non-batch option to have cron code is running by the script itself: in this case cron code will be run each time someone access your home page. Such option is called "pesudo" oir "non-batch".

Cron code may be released in protected/models/Cron.php class or in each module in protected/module-name/models/ModuleName.php class with method cron().