Set cron job php Set cron job php codeigniter codeigniter

Set cron job php


Here is a nice article for managing your Cron jobs from PHP

http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/

this example contain full description of how to write a Cron job from PHP which is manageable from interface with complete source code.


Cron is a job scheduler for linux. It will not work on windows (unless you start getting fancy with Cygwin and etecetera). If you must support it system-wide, across an variety of unknown systems and all in PHP (that cannot schedule things as is) you can resort to any one of these three used solutions:

Calls from Ground Control
(best on resources, concrete times, most elegant solution)

  1. Have your own box running cron and ssh'ing other machines to run commands
  2. Have your own box running cron and accesing "secret url's" ro run commands
  3. Have an external service, like SetCronJob call your "secret url's"

Random Job Runner
(the easiest to implement, moderate on resources)

If you need to do something that will not directly affect the way users behave on your site and it's not time-sensitive (just needs to be done) you can resort to having it run with a statistical probability.

// this will run on average once every 100 page viewsif(rand(1, 100)==1) runJobQueue();

Worst Case Scenario
(harder on resources)

If your job must be done every so often because it will directly afect the user and it must meet some time schedule you can save query and update a database with the last run timestamp and keep checking on that to see if the job must be re-run

// if has passed 1 hour since last update... re-update it$result = mysql_query("SELECT `timestamp` FROM job_times WHERE whatever...")if(mysql_result($result, 2) >= ( now() - 3600)) {    runJobQueue()


cron job is available to linux, the alternative in windows is schedule task.http://support.microsoft.com/kb/308569