Cron jobs in codeigniter

有些话、适合烂在心里 提交于 2019-11-28 10:22:22

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