Cron jobs in codeigniter

别来无恙 提交于 2019-11-27 03:37:42

问题


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?


回答1:


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.




回答2:


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.




回答3:


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



回答4:


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



回答5:


just use this command and paste it.

wget www.example.com/index.php/controller/function


来源:https://stackoverflow.com/questions/7322378/cron-jobs-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!