-
Notifications
You must be signed in to change notification settings - Fork 0
Cron job bootstrapper
Category:Config | Category:Config::Community | Category:Config::Cron
I've put together this simple bootstrapper file that you can use to directly run your CodeIgniter controllers from the commandline. It's a [b]very easy[/b] and elegant solution for using CI controllers for cron jobs. It also supports [b]logging[/b].
[b][url=http://codeigniter.com/wiki/634a97f1a882ae03fd871f15f33e98ed/]Download[/url][/b]
Note: this file must have UNIX-style line breaks. If you try to run it and it complains "Invalid interpreter: /usr/bin/php^M" then you probably need to [url=http://www.tech-recipes.com/unix_tips200.html]convert your line breaks[/url].
[code] #!/usr/bin/php <?php
| /* |
|---|
| CRON JOB BOOTSTRAPPER |
| @author Jonathon Hill (jonathon AT compwright DOT com) |
| -------------------------------------------------------------- |
| | This section is used to get a cron job going, using standard | CodeIgniter controllers and functions. | | 1) Set the CRON_CI_INDEX constant to the location of your | CodeIgniter index.php file | 2) Make this file executable (chmod a+x cron.php) | 3) You can then use this file to call any controller function: | ./cron.php --run=/controller/method [--show-output] [--log-file=logfile] [--time-limit=N] | | GOTCHA: Do not load any authentication or session libraries in | controllers you want to run via cron. If you do, they probably | won't run right. | */
define('CRON_CI_INDEX', 'index.php'); // Your CodeIgniter main index.php file
define('CRON', TRUE); // Test for this in your controllers if you only want them accessible via cron
$script = array_shift($argv);
$cmdline = implode(' ', $argv);
$usage = "Usage: cron.php --run=/controller/method [--show-output][-S] [--log-file=logfile] [--time-limit=N]\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;
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_time_limit(CRON_TIME_LIMIT);
ob_start();
require('index.php'); // Main CI index.php file
$output = ob_get_contents();
if(CRON_FLUSH_BUFFERS === TRUE) {
while(@ob_end_flush()); // display buffer contents
} else {
ob_end_clean();
}
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);
echo "\n\n";
?>
[/code]
- Original author: Derek Jones
- How to extend helpers: See User Guide
- Modified by: Thomas Stapleton (id, classes, selected country option and all option)
- Modified by: Bradley De-Lar (construct, setLayout example)