November 24, 2013

November 24, 2013
2
In this article we are going to discuss about, How to execute Cron Job in CodeIgniter (CI) using PHP. Codeigniter (CI) runs based on the front controller pattern where all the requests are routed using the index.php file. So we need to go through the routing procedures  to execute any code on CodeIgniter (CI) application. Basically if we need to run cron job, we need to create the code files and placed it in the server and call the files in the cron job using path to files.

But in CodeIgniter (CI) applications, it is not much easy to call the cron job files and use CodeIgniter models inside this scripts. You need to load all the required models and files manually. We cannot achieve this using normal php script calling procedure.

Curl:

PHP Curl extension is used to transfer data from server to another server using various methods. In this example we need to execute a remote url using normal http method in curl.

Steps to install Curl in Windows:

You have to allow the curl extension by removing ';' in the php.ini file and restart apache server.

Steps to install Curl in Fedora:

Type the following command in the terminal using the super user.

yum install php-curl

Steps to install Curl in Ubuntu:

Type the following command in the terminal using the super user.

apt-get install php-curl

Setting the Cronjob using Curl

You can set up the cronjob to call the CodeIgniter (CI) url using the following line of code.

/usr/bin/curl http://www.example.com/controller/cron_function


Open the crontab using the terminal and enter the above line of code and restart the cron. Use the path to the curl file according to your installed location. Call the cron function of your site remotely using http.

2 comments: