How to set up a cron job with codeigniter

Pooshonk picture Pooshonk · Jul 17, 2013 · Viewed 8.7k times · Source

I am trying to set up a cron job using COdeigniter but I cannot figure out how to get it to work. I have a file called email_check.php in my controllers folder, and I have added a .cron file to the servers cron folder, which contains the following

email_check.cron

*/1 * * * * php /var/www/html/application/controllers/email_check

email_check.php

class Email_check extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->index();
    }

    function index()
    {
        $this->load->model('admin/info_model'); 
        $this->info_model->addTestData();
    }

}

The addTestData adds a new row into a database table. I would like this to run every minute, however it isn't working at all and I have no idea why.

Maybe it may be the paths that are wrong. Do I need to point the php part to the php.exe in the server.

If anyone could help or point me in the right direction it would be greatly appreciated!

Answer

Wolf picture Wolf · Jul 17, 2013

To use CodeIgniter via the commmand line, you need to call the index.php file and pass in the controller and method as arguments, plus any other arguments. So at the minimum the cron job call would be:

~/public_html/sitefolder/index.php controller method

Or using the path to your application index file. But, you also need to use PHP compiled for the command line, not just PHP for CGI-FCGI. So your call might be something like:

/ramdisk/bin/php5-cli ~/public_html/sitefolder/index.php controller method

Depending on where your PHP CLI is located.