Last night, I was trying to get a crontab working in CodeIgniter, since my current project (a personal project) is using CodeIgniter now. I didn’t wanted to use the conventional approach of running crontabs and my code seperate.
Unfortunately, CodeIgniter does not have support for Crontabs and I didn’t wanted to user wget in linux crontab to initiate unwanted requests.
I spent some time and came up with a solution. It uses
- CodeIgniter backend
- Does not modify any core architecture of CodeIgniter
- Keeps configuration at one place
So enough of the summary and let me show you how its done.
Create a file (e.g. cron.php) in the same place as your index.php and system folder. Here is its code
/** * @author Asim Zeeshan * @web http://www.asim.pk/ * @date 13th May, 2009 * @copyright No Copyrights, but please link back in any way */ /* |--------------------------------------------------------------- | CASTING argc AND argv INTO LOCAL VARIABLES |--------------------------------------------------------------- | */ $argc = $_SERVER['argc']; $argv = $_SERVER['argv']; // INTERPRETTING INPUT if ($argc > 1 && isset($argv[1])) { $_SERVER['PATH_INFO'] = $argv[1]; $_SERVER['REQUEST_URI'] = $argv[1]; } else { $_SERVER['PATH_INFO'] = '/crons/index'; $_SERVER['REQUEST_URI'] = '/crons/index'; } /* |--------------------------------------------------------------- | PHP SCRIPT EXECUTION TIME ('0' means Unlimited) |--------------------------------------------------------------- | */ set_time_limit(0); require_once('index.php'); /* End of file test.php */
Now, we need a controller e.g. test so the controller code could be something like this. Please note that normally we do not need to output anything from these controllers since they are doing some background work or sending emails but for the sake of giving an example, I will be output-ing something to elaborate the example.
/** * @author Asim Zeeshan * @web http://www.asim.pk/ * @date 13th May, 2009 * @copyright No Copyrights, but please link back in any way */</code> class Test extends Controller { function __construct() { parent::Controller(); } function index() { echo "testing from index \n"; } function test() { echo "testing from test \n"; } }
Now execute crontab on linux command prompt
php /full-path-to-cron-file/cron.php /test/index
Voila! all you need now, is to setup the crontab like you normally do.
This code is not shared under any license so feel free to copy/modify/use it. Please link back to this website / post in any way e.g. direct link, credits etc.
P.S. The examples above user PHP5 constructor, If you need to execute this code on PHP4, please modify the constructors.
Hi Asim! Thanks for a good artilce, i also love CodeIgniter, but i'am sure that invoking wget by cron is a real "unix way".
Have a good day.
Yes, thats an easy way but why do we need to initiate wget and an unnecessary http request when we can do it from within the system? Thats why I came up with this tutorial.
"invoking wget by cron is a real “unix way”.
No It was never…
Of course, it is easy to have them under the HTTP path but most of the times I have realized that making them secure is more necessary. At times if you have not performed proper checks then unwanted access to the cron URLs can get you in trouble.
Just as a side note: CodeIgniter wiki has an article about setting up cron jobs in CI. I used that somewhere in 2007.
Best regards,
I really like the way this would work but for some reason I am getting no output once the Core Codeigniter Class is loaded. My script seems to run but gives me no output or errors. I do get output if i echo something before:
require_once BASEPATH.'codeigniter/CodeIgniter'.EXT;
but nothing after that.
Any ideas?
Thank you
there is more one note. you should chmod directories where framework(or its component)could create or modify files
Nice work ..!!
My OS is Xp, how can i execute crontab on linux command prompt help me
There is a nice cron bootstrap on the codeigniter wiki http://codeigniter.com/wiki/Cron_job_bootstrapper/
that’s worth checking out
Thanks Dave for sharing the link with everyone. I appreciate that.
Nice post…will be helpful for me..one question though…
can we use the CI components (email,database) in the this?
Yes, you can use everything that you can use in normal CI
Thanks very much. It is very easy way. I have tried many solutions before and I think this is the best solution I found.
Your most welcome 🙂
Excellent post. By the way, it's spelled "voila", not "viola" 🙂
Thanks for the corrections 🙂
Oh by the way, this doesn't seem to work with Codeigniter 2+
Nice Post….. will be helpful for me. Thanks…………
[…] I’ve attempted to create a bootstrap for my cron needs and done extensive research into this topic, to no avail. One of the solutions attempted: http://www.asim.pk/2009/05/14/creating-and-installing-crontabs-using-codeigniter/ […]
Nice post. i tried in many ways. this works fine now.
I am using cron to send mail . i am getting the mail . But an extra mail is coming with the following error
PHP Notice: Undefined index: HTTP_HOST in httpdocs/index.php on line 131
PHP Notice: Undefined variable: region_prefix_var in httpdocs/index.php on line 135
PHP Notice: Undefined index: HTTP_HOST in httpdocs/application/config/config.php on line 23
A PHP Error was encountered
Severity: Notice
Message: Undefined index: HTTP_HOST
Filename: config/config.php
Line Number: 23
A PHP Error was encountered
Severity: Notice
Message: Undefined index: HTTP_HOST
Filename: config/config.php
Line Number: 23
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information – headers already sent by (output started at httpdocs/system/core/Exceptions.php:185)
Filename: libraries/Session.php
Line Number: 672
A PHP Error was encountered
Severity: Notice
Message: Undefined index: HTTP_HOST
Filename: hooks/subdomain_check.php
Line Number: 15
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information – headers already sent by (output started at httpdocs/system/core/Exceptions.php:185)
Filename: hooks/subdomain_check.php
Line Number: 45
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information – headers already sent by (output started at httpdocs/system/core/Exceptions.php:185)
Filename: libraries/Session.php
Line Number: 672
Set php’s error_reporting correctly and suppress the NOTICES / INFOS / WARNINGS and DEPRECIATED errors (if in production mode)
Thank for, this article is so much use full for set cron on CodeIgniter