Many of us faced the problem of running CodeIgniter controllers from Cron or from Command Line. usually we write the following codes to run a php file from cron/CLI.
php /var/www/path/to/your/script
but if we give CodeIgniter Controller path here it will not work since CodeIgniter will fail to initialize its core classes. A work around of this is running the script using CURL
curl http://yourwebsite/controllername
the problem with the above command is that it takes too much CPU resources .
Today my collegue Hasan showed me a nice way of doing it, just write the following codes in a php file in your root folder. here is the way to go
<?
$_GET[“/external/do_cron”] = null;
require “index.php”;
?>
note: here “external” is the controller name and “do_cron” is the function name that you want to execute.
now save this file to any name you want (eg. mycron.php) and run the script using
php /var/www/path/to/script
[here it is php /var/www/mywebsite/mycron.php]
if the above code does not work, you can try the following one. (thanks to sucio)
$_SERVER[“REQUEST_URI”] =”external/do_cron”;
require “index.php”;
I tried it but it didnt work, This is the code that worked
$_SERVER[“REQUEST_URI”] =”external/do_cron”;
require “index.php”;
cocksmack
$_GET[“/external/do_cron”] = null;
require “index.php”;
worked great!
Thank you!
http://www.asim.pk/2009/05/14/creating-and-installing-crontabs-using-codeigniter/
Check this tutorial that I have written about CodeIgniter Crontabs. It will definitely help you.
http://codeigniter.com/wiki/Category:Advanced::CronScript/
– here is solution.
good post, thanks, it’s exactly what i needed.
hi boolean dreams
your original code worked.
thanks a lot
drsjohnsam@gmail.com
suppose i want to pass arguments means how to send .it s not working.