[eluser]CroNiX[/eluser]
Here's what I have for my cron.php, which is in the same location as CI's index.php (with hardened permissions). Note the shebang with full path to php before php opening. I've disabled the logging in mine as I do the logging from my CI cron mothods, so it's commented out.
Code:
#!/usr/bin/php5
<?php
//Note: --server: is raw server name without http://
define('CRON_CI_INDEX', dirname(__FILE__) . '/index.php');
define('CRON', TRUE); // Test for this in your controllers if you only want them accessible via cron
# Parse the command line
$script = array_shift($argv);
$cmdline = implode(' ', $argv);
$usage = "Usage: cron.php --run=/controller/method [--show-output][-S] [--log-file=logfile] [--time-limit=N] [--server=http_server_name]\n\n";
$required = array('--run' => FALSE);
foreach($argv as $arg)
{
list($param, $value) = explode('=', $arg);
switch($param)
{
case '--run':
// Simulate an HTTP request
$_SERVER['PATH_INFO'] = $value;
$_SERVER['REQUEST_URI'] = $value;
$required['--run'] = TRUE;
break;
case '-S':
case '--show-output':
define('CRON_FLUSH_BUFFERS', TRUE);
break;
case '--log-file':
if(is_writable($value)) define('CRON_LOG', $value);
else die("Logfile $value does not exist or is not writable!\n\n");
break;
case '--time-limit':
define('CRON_TIME_LIMIT', $value);
break;
case '--server':
$_SERVER['SERVER_NAME'] = $value;
$_SERVER['HTTP_HOST'] = $value;
break;
default:
die($usage);
}
}
if(!defined('CRON_LOG')) define('CRON_LOG', 'cron.log');
if(!defined('CRON_TIME_LIMIT')) define('CRON_TIME_LIMIT', 0);
foreach($required as $arg => $present)
{
if(!$present) die($usage);
}
# Set run time limit
set_time_limit(CRON_TIME_LIMIT);
# Run CI and capture the output
ob_start();
chdir(dirname(CRON_CI_INDEX));
require(CRON_CI_INDEX); // Main CI index.php file
$output = ob_get_contents();
if(defined('CRON_FLUSH_BUFFERS')) {
while(@ob_end_flush()); // display buffer contents
} else {
ob_end_clean();
}
# Log the results of this run
// error_log("### ".date('Y-m-d H:i:s')." cron.php $cmdline\n", 3, CRON_LOG);
// error_log($output, 3, CRON_LOG);
// error_log("\n### \n\n", 3, CRON_LOG);
Here's my actual cron job set to run at midnight (although it doesn't show that). Note the full path to php is used, the controller (in --run) starting with a slash, and how the server name is entered:
Code:
/usr/bin/php5 -q /home/site/public_html/cron.php --run=/cron/daily --server=www.site.com