简体   繁体   中英

How to set up a cron job with codeigniter

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!

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.

This won't work because just hitting your email_check.php controller won't do anything because its not going to call your index() method.

You want to either write a script that is going to create a new instance of the controller and call the method or call it via a URL, something like this I think

* * * * * wget http://sitename.com/email_check

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM