Skip to content

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.

15 Comments
  1. 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. :)

  2. Works perfectly. Thanks!

  3. Thanks Jonathon! Worked first time.

  4. –show-output will need to be something with equal sign.

    –show-output=1 worked.

  5. David permalink

    I get an error as I am using mod rewrite to remove index.php

    PHP Notice: Undefined index: HTTP_HOST in /var/www/vhosts/xxxx.com/httpdocs/application/config/config.php on line 15

    any work around?

  6. In CI 2.0 I needed to change $config['uri_protocol'] to PATH_INFO as CI was using the wrong method for the script in AUTO mode.

    There’s some minor bugs on the script but that doesnt compromise this awesome clean way to run CodeIgniter Cron Jobs.

    Fully working.

  7. Thanks for posting this work, it has been a great help to me getting CRON and CI to play nice together.

  8. @David: work around the .htaccess issue by hard coding your HTTP_HOST in cron.php. Around line 44 add something similar to:

    $_SERVER['HTTP_HOST'] = ‘http://localhost/index.php/';

  9. Nice script. It broke in v2 though. But then again: v2 runs CLI smoothly out of the box :-)
    http://codeigniter.com/user_guide/general/cli.html

  10. Tim O'Hare permalink

    Thanks for this. I love the concept, however, I have not been able to successfully make it work on my end. I’ve posted my question to both StackOverflow and Quora so I’ll simply link to them here. Please feel free to answer the question in either location.

    http://stackoverflow.com/questions/6200828/issues-with-codeignitor-and-cron-bootstrapper

    http://www.quora.com/Why-will-Codeignitor-Cron-Bootstrapper-not-work-with-my-CI-setup

    Thanks in advance for your help.

    -Tim

  11. how to put schedule date on it.

  12. i would like to run my cron on 10am every day. is it possible?

  13. Jonathon permalink

    @Joseph – See http://unixgeeks.org/security/newbie/unix/cron-1.html, esp. the section “Using cron”.

  14. Nader permalink

    very useful script.

    1 Question: can be used to run CI with HMVC ?

    ie. can I use:

    – run [module name]/[controller]/[method]

    I haven’t tried it, it will probably work, just thought to bring up the matter.

  15. Jonathon permalink

    You can, but not out of the box. Google “CI HMVC”, there are plugins out there to enable it.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS