Cron Bootstrapper
This is a simple bootstrapper file that you can use to directly run your CodeIgniter controllers from the commandline. It’s a very easy and elegant solution for using CI controllers for cron jobs. It also supports logging.
Features
- Easy to use
- No core framework hacking
- Bootstrap file can be stored anywhere on the server, independently of your application
- Max. runtime option
- Logging: store the results of each run
- Bootstrap file can be run directly without the PHP command (on UNIX systems only)
Download
Download here or view the source code.
Installation
1) Download cron.php and save it anywhere on your server (outside of the document root).
2) Set the CRON_CI_INDEX constant to the full absolute file/path of your CodeIgniter index.php file
define('CRON_CI_INDEX', '/var/www/vhosts/my-codeigniter-application/index.php');
3) Make this file directly executable:
chmod a+x cron.php
4) You can now use this file to call any controller function:
cron.php --run=/controller/method
Command options
- –run=/controller/method
- (Required) The controller and method you want to run.
- –show-output
- Display CodeIgniter’s output on the console (default: don’t display)
- –log-file=logfile
- Log the date/time this was run, along with CodeIgniter’s output
- –time-limit=N
- Stop running after N seconds (default=0, no time limit)
- –server=http_server_name
- Set the $_SERVER[‘SERVER_NAME’] system variable (useful if your application needs to know what the server name is)
Troubleshooting
“I get this error when I try to execute ./cron.php:”
Invalid interpreter: /usr/bin/php^M
This is caused by not having UNIX-style line breaks, which usually happens by copying files created on other operating systems. Use this command to convert the line breaks to UNIX format:
mv cron.php cron.old
tr -d '1532' < cron.old > cron.php
rm cron.old
cron.php bombs around line 111: require(CRON_CI_INDEX);
This would happen if you don’t have the path to your application’s main index.php defined correctly at line 24:
// Your CodeIgniter main index.php file
define('CRON_CI_INDEX', '/var/www/vhosts/intranet/index.php');
“I don’t get any errors or output and the script doesn’t seem to run. What would cause this?”
Apparently some authentication libraries, such as Michael Wales’ Erkana authentication library, cause this sort of behaviour (it’s possible that any code that depends on sessions could cause this). Just use some conditional logic testing to see if the CRON constant is defined before auto-loading any authentication libraries.
Hi Jonathon,
I think this is a cool solution. I have a few comments:
1) the source code provided in the download links, does not match the documentation: no –server parameter. Of course, this is not a show stopper.
2) My hosting service didn’t work with cron.php as a standalone script. so I ran it with “php PATH_TO/cron.php –run=/controller/method”, and removed the shebang.
3) I made logging optional. In the provided code, if no –log-file option is provided, it stills logs to “cron.log”
Anyway, thanks a lot for open sourcing the script.
Works perfectly. Thanks!