How to Run Cron Jobs in CodeIgniter

After a fair bit of trawling Google, I got the impression that setting up a cron job in codeigniter was going to be a bit tricky.

The two potential solutions I discovered were:

  1. execute the “wget” command to replicate a normal browser request, which then exposes my cron functionality to the big wide world and creates some additional unnecessary overheads, or
  2. Create a bespoke front controller a bit like the existing index.php but specifically for cron jobs

Neither of these options sound particularly ideal to me, and thankfully, there is a better way (and it turned out to be a case of RTFM).

Firstly, just create a normal controller. I creatively called mine Cron.

class Cron extends CI_Controller {

    function __construct()

        // this controller can only be called from the command line
        if (!$this->input->is_cli_request()) show_error('Direct access is not allowed');

    function foo($bar = 'bar')
        echo "foo = $bar";

Note the call to $this->input->is_cli_request() which ensures that this controller cannot be accessed directly from a url.

Next we setup our cron job as you would any other, except that you provide the path in a special format.

php /path/to/index.php controller_name method_name [“params”]

The first part is either the command “php” or your server path to php.

The second part is the absolute path to your CI front controller, eg. /path/to/index.php.

The third part is the controller name, followed by the method name, followed by optional parameters.

The CI manual gives the example:

php /path/to/index.php cron foo

If that doesn’t work, it probably means you don’t have the package php5-cli installed. On debian / ubuntu you can install this package as follows:

sudo apt-get install php5-cli

If you are not able to install packages, you can specify your path to php. I set mine as follows:

/usr/local/bin/php -f /home/clinic/public_html/index.php cron foo

You can even pass parameters in quotes, which have the same effect as parameters in regular url requests!

/usr/local/bin/php -f /home/clinic/public_html/index.php cron foo “beer”

15 Replies to “How to Run Cron Jobs in CodeIgniter”

  1. How would you debug this? As in, printing or echo’ing doesn’t appear to dump any information out to STDIN.

  2. Hey, thanks for useful article, however I can’t get my cron jobs to work. When I am trying to access the “/usr/bin/php -f home/my_site/index.php cron index” , I am receiving my main controller data. I can’t access the cron.php controller. Seems, like the parameters after the index.php isn’t going anywhere. What’s the problem?

  3. Very nice article, I have done with “/usr/local/bin/php -f /home/username/public_html/index.php croncontrollername cronfunctionname

    Keep posting good stuff dear…

    Thanks a lot

  4. /web/cgi-bin/php5_3 ~/html/index.php controller function

    im getting the html of my home page as a error

    im running this cmd in goddady cron job

  5. I dude …I read your artical… That is nice…But I am not able to run my cron job properly ..Can you help me out .

    I am using window cpanel in godaddy.

    Also Codigniter i m using…
    I want to setup cron job for sending newsletter to my subscriber…

    Plz let me know the process for that.


  6. Thank you!!!

    This worked for me.
    /usr/local/bin/php -f /home1/jazzplay/public_html/ autoemail

  7. Scheduling cron jobs in PHP is a lot easier if you are using Cloudways platform instead of cPanel. When you logged in to their platform, there is button “Add Cron Job” under Cron Job section. Just click on the button, set its frequency using the drop down and provide the link to the script of the task you want to schedule and click the Apply button. Just like that, within few minutes, your cron job will be all set. Here is an example:
    If you want to manually set the cron job, then you can do that as well using the Advanced section

  8. I have to run Cron job from my CodeIgniter project.
    Cron is written in the controller and I have to run it.

    I have tried with above options but it’s not working please let us know.

Leave a Reply

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