Introduction to Cronjobs in Codeigniter
Considering above points its better to write the code for the cronjob normally inside codeigniter controllers and call it remotely using the crontab. We cannot achieve this using normal php script calling procedure. Hence we need to use curl extension to call the codeigniter url remotely using the cron. Next section I’ll show you how to set up cronjob using curl extension.
Using Curl in Executing Cronjob
Introduction to 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.
Installing Curl
Installing method of curl various according to the operating system. I’ll show you how to install curl in some of the popular operating systems.
Windows
You have to allow the curl extension by removing ‘;’ in the php.ini file and restart apache server.
Fedora
Type the following command in the terminal using the super user.
yum install php-curl
Ubuntu
Type the following command in the terminal using the super user.
apt-get install php-curl
Setting the Cronjob using Curl
*/5 * * * * /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.
Leave a Reply