How to run a CodeIgniter file through CRON?


I've tried the following method in the past:

$_SERVER['PATH_INFO'] = 'cron/controller/index';
$_SERVER['REQUEST_URI'] = 'cron/controller/index';

and putting this in a file in the codeigniter installation directory, calling it cron.php, and then invoking it via:

php /home/[username]/public_html/my_project/cron.php

If I type the URL to cron.php in my browser it works perfectly, however whenever its run via CRON I get a 404 error. Putting the following code in the show_404() function of CodeIgniter

function show_404($page = '')
   echo "\n\n";
   die ($page);

results in getting the following output emailed to me:

   [SHELL] => /bin/sh
   [MAILTO] =>
   [USER] => [me]
   [PATH] => /usr/bin:/bin
   [PWD] => /home/[me]
   [SHLVL] => 1
   [HOME] => /home/[me]
   [LOGNAME] => [me]
   [_] => /usr/bin/php
   [PHP_SELF] =>
   [REQUEST_TIME] => 1266479641
   [argv] => Array
           [0] => /home/[me]/public_html/my_project/cron.php

   [argc] => 1
   [PATH_INFO] => cron/controller/index
   [REQUEST_URI] => cron/controllers/index


Here I've [me] in place of my actual username.

Any ideas?

2/18/2010 8:05:30 AM

Accepted Answer

The simplest way to run a cron via CodeIgniter is to make a cron URL available via your app.

Then call it via wget

wget -O - -q -t 1

Inside the controller you can then use a log to ensure the cron is not run too often i.e. if the Google robots trigger it by mistake.

A second method would be to use lynx

/usr/local/bin/lynx -source
2/18/2010 8:21:13 AM

You may also like to add --spider to ignore the response. This stops the request from timing out:

wget -O - -q -t 1 --spider

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow