我正在尝试与CodeIgniter内置的网站做cron作业-我已经访问了CPanel cron功能,任何人都可以建议使用CPanel设置cron作业的最佳方法?
我正在使用CodIgniter,所以不能确定如何在cron作业中调用控制器?
E。g http://admin.com/sites/publish/
我如何使用cron作业访问站点控制器中的此发布功能?
最好的方法是从cron作业中的命令行调用…
php /path/to/index.php controller >> /dev/null
您可以在CI中通过命令行运行控制器,参见这里。
对我来说,更简单的方法是使用cURL并在cron中执行url:
curl http://admin.com/sites/publish/
如果你需要保护url,你可以通过post发送数据:
curl -X POST -d "apikey=yourapikey&another=variable" http://admin.com/sites/publish/
我这样做,创建文件夹cron
/application
/cron
my_task.php
/public
为每个cron作业制作脚本/cron/my_task.php,内容为
<? $_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
?>
将控制器设置为Cron,但在__construct
中添加IP验证最后像
一样运行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
对于Cronjob,尝试这样访问命令行控制器、函数和参数:
php index.php/controller/function/param1/param2/param3 etc
或
php index.php controller function param1 param2 param3 etc
直接用这个命令粘贴。
wget www.example.com/index.php/controller/function