I am trying to do a cron job with a site built in CodeIgniter - I've got access to the CPanel cron feature can anyone suggest the best way to setup a cron job using CPanel?
I am using CodIgniter so cannot be sure how to call a controller within a cron job?
E.g http://admin.com/sites/publish/
How would I access this publish function within the sites controllers using a cron job?
Best way is to call from the command line in the cron job...
php /path/to/index.php controller >> /dev/null
You can run controllers via the command line in CI, see here.
For me the easier way of doing this is using cURL and executing the url in the cron:
curl http://admin.com/sites/publish/
If you need to secure the url, you could send data via post using:
curl -X POST -d "apikey=yourapikey&another=variable" http://admin.com/sites/publish/
This way you don't have to fight with php parameters and different configurations.
I do this such way,
create folder cron
/application
/cron
my_task.php
/public
make script for each cron job /cron/my_task.php with content
<? $_SERVER["SCRIPT_URL"] = "/controllerName/MethodName"; // you can set url in routes if you want
$_SERVER["HTTP_HOST"] = "your_site_address.com"; // without http://www
require(dirname(__FILE__) . "/../public/index.php"); // path to index.php
?>
make controller Cron like others, but add validation on IP in __construct
and finaly run like
1 10 * * * cd /path_to_site_folder/cron/ && usr/local/bin/php /path_to_site_folder/cron/my_task.php >> path_to_log/some.log
For Cronjob try this to access command line controller, functions and params:
php index.php/controller/function/param1/param2/param3 etc
or
php index.php controller function param1 param2 param3 etc
just use this command and paste it.
wget www.example.com/index.php/controller/function