codeigniter Cronjob in Codeigniter


Example

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cron extends CI_Controller 
{
    /**
     * This is default constructor of the class
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->library('input');
        $this->load->model('cron_model');
    }
    
    /**
     * This function is used to update the age of users automatically
     * This function is called by cron job once in a day at midnight 00:00
     */
    public function updateAge()
    {            
        // is_cli_request() is provided by default input library of codeigniter
        if($this->input->is_cli_request())
        {            
            $this->cron_model->updateAge();
        }
        else
        {
            echo "You dont have access";
        }
    }
}
?>

Call this from your cpanel/cron manager as follows (I added more ways to call it):

0 0 0 0 0 php-cli /home/your_site_user/public_html/index.php cron updateAge

OR

0 0 0 0 0 wget http://your_site_url/cron/updateAge

OR

0 0 0 0 0 /usr/bin/php /home/your_site_user/public_html/index.php cron updateAge

In my case: wget thing is working on plesk and cpanel (wget creating files on server in your root directory). php-cli works on plesk and cpanel both.