Cron Bootstrapper

This is a simple bootstrap file that you can use to directly run your CodeIgniter controllers from the command line and redirect the output to a log file.

Note: While CodeIgniter 2.0 introduced CLI support, its syntax is less attractive and does not support logging or setting time limits.

Features

  • Easy to use
  • No core framework hacking
  • Bootstrap file exists independently of your application
  • Supports runtime limit
  • Log the results of each run
  • Run directly without the PHP command (on UNIX systems only)

Download

Get it on Github.com.

19 thoughts on “Cron Bootstrapper”

  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. 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?

  3. 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.

  4. @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/';

  5. 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

  6. 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.

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

  8. Warning: array_shift() [function.array-shift]: The argument should be an array in /home/content/63/8862763/html/cron.php on line 54

    Warning: implode() [function.implode]: Invalid arguments passed in /home/content/63/8862763/html/cron.php on line 55

    Warning: Invalid argument supplied for foreach() in /home/content/63/8862763/html/cron.php on line 58
    Usage: cron.php –run=/controller/method [--show-output][-S] [--log-file=logfile] [--time-limit=N] [--server=http_server_name]

    .. please help me to solve it …

  9. hi, i am using your cron, anw i have a question:

    this is the default :
    cron.php –run=/controller/method

    if i want to adding log, how to type it?
    cron.php –run=/controller/method –logfile

    i am i wrong?

    thanks

  10. Posted in SO but no replies as yet – please could you take a look?
    http://stackoverflow.com/questions/13281407/codeigniter-cronjob-cpanel-command-line-using-bootstrapper

    I’ve just set up a Cronjob using the bootstrapper on GitHub (mentioned on here but not this specific issue). https://github.com/EllisLab/CodeIgniter/wiki/Cron-job-bootstrapper

    As per guidance I have defined the CI index.php as below

    define(‘CRON_CI_INDEX’, ‘/home/mysite/public_html/index.php’);
    As a new user to the syntax, I’m not sure I’m entering the correct information on the command line. I have:

    php -q -f /home/mysite/public_html/cronjobbootstrapper/cron.php –run=/testing/email_sending_function
    And various other variations without the — before run= etc. I’m not sure what I’m doing wrong here, it’s not particularly clear in the guidance but others on here have used it successfully! I simply get an email through from the server containing part of the file contents. Usage: cron.php –run=/testing/mail_send_greg [--show-output][-S] [--log-file=logfile] [--time-limit=N] [--server=http_server_name]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Hello! I’m a self-taught freelance web developer, and this is my blog. Here, I document problems and solutions in PHP, MySQL, and Javascript that I encounter along the way. Enjoy!