Creating a cronjob with Codeigniter

You need cronjob to achieve many things. For example, if you need to reset/update the database every 30min or one hour, you need cronjob.

First create a controller in the folder application/controller/. I create a controller like this application/controller/crontodo.php
class Crontodo extends CI_Controller{

 function __construct()
 {
  parent::__construct();
  //load the model
  $this->load->model('cronjob_model','',TRUE);
 }

 function index()
 {
  $this->cronjob_model->my_cronjob();
 }
}
The next one is to create the model as application/models/cronjob_model.php
class Cronjob_model extends CI_Model
{
  function __construct()
  {
  // Call the Model constructor
  parent::__construct();    
  }

         function my_cronjob(){

          // here you should add what you wanna do with cronjob such as drop, create, add, delete. update etc

          }
}
And you can set up a cron job through a crontab.You need to add a URL, for example http://www.example.com/index.php/crontodo

Comments

  1. So where do you set the cron job time or interval of execution

    ReplyDelete

Post a Comment

Popular Posts