php 如何通过 CRON 运行 CodeIgniter 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2287070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a CodeIgniter file through CRON?
提问by Click Upvote
I've tried the following method in the past:
我过去尝试过以下方法:
<?php
set_time_limit(0);
$_SERVER['PATH_INFO'] = 'cron/controller/index';
$_SERVER['REQUEST_URI'] = 'cron/controller/index';
require_once('index.php');
?>
and putting this in a file in the codeigniter installation directory, calling it cron.php, and then invoking it via:
并将其放在 codeigniter 安装目录中的一个文件中,将其命名为 cron.php,然后通过以下方式调用它:
php /home/[username]/public_html/my_project/cron.php
If I type the URL to cron.php in my browser it works perfectly, however whenever its run via CRON I get a 404 error. Putting the following code in the show_404()function of CodeIgniter
如果我在浏览器中输入 cron.php 的 URL,它可以完美运行,但是每当它通过 CRON 运行时,我都会收到 404 错误。在show_404()CodeIgniter的函数中放入如下代码
function show_404($page = '')
{
print_r($_SERVER);
echo "\n\n";
die ($page);
}
results in getting the following output emailed to me:
结果将以下输出通过电子邮件发送给我:
Array
(
[SHELL] => /bin/sh
[MAILTO] => [email protected]
[USER] => [me]
[PATH] => /usr/bin:/bin
[PWD] => /home/[me]
[SHLVL] => 1
[HOME] => /home/[me]
[LOGNAME] => [me]
[_] => /usr/bin/php
[PHP_SELF] =>
[REQUEST_TIME] => 1266479641
[argv] => Array
(
[0] => /home/[me]/public_html/my_project/cron.php
)
[argc] => 1
[PATH_INFO] => cron/controller/index
[REQUEST_URI] => cron/controllers/index
)
home/[me]
Here I've [me] in place of my actual username.
在这里,我用 [me] 代替了我的实际用户名。
Any ideas?
有任何想法吗?
回答by Jon Winstanley
The simplest way to run a cron via CodeIgniter is to make a cron URL available via your app.
通过 CodeIgniter 运行 cron 的最简单方法是通过您的应用程序提供 cron URL。
Then call it via wget
然后通过 wget 调用它
wget -O - -q -t 1 http://www.example.com/cron/run
Inside the controller you can then use a log to ensure the cron is not run too often i.e. if the Google robots trigger it by mistake.
在控制器内部,您可以使用日志来确保 cron 不会过于频繁地运行,即谷歌机器人是否错误地触发了它。
A second method would be to use lynx
第二种方法是使用lynx
/usr/local/bin/lynx -source http://www.example.com/cron/run
回答by Sharif Macky
You may also like to add --spider to ignore the response. This stops the request from timing out:
您可能还想添加 --spider 以忽略响应。这会阻止请求超时:
wget -O - -q -t 1 --spider http://www.example.com/cron/run
回答by Shomz
You might also want to check this out: Cron job bootstrapper
你可能还想看看这个:Cron job bootstrapper
This is a simple bootstrapper file that you can use to directly run your CodeIgniter controllers from the commandline. It's a very easy and elegant solution for using CI controllers for cron jobs. It also supports logging.
这是一个简单的引导程序文件,可用于从命令行直接运行 CodeIgniter 控制器。对于将 CI 控制器用于 cron 作业,这是一个非常简单和优雅的解决方案。它还支持日志记录。
回答by Phil Sturgeon
There is a wiki article about how to run CodeIgniter on the command line, but this is more useful for applications that need to interact with the user through terminal (there's a library for that too).
有一篇关于如何在命令行上运行 CodeIgniter 的 wiki 文章,但这对于需要通过终端与用户交互的应用程序更有用(也有一个库)。
http://codeigniter.com/wiki/CI_on_the_command_line/
http://codeigniter.com/wiki/CI_on_the_command_line/
One benefit of doing it this way over using wget is you can protect your code from being run by users or bots with:
与使用 wget 相比,这样做的好处之一是您可以通过以下方式保护您的代码不被用户或机器人运行:
if(!empty($_SERVER['HTTP_HOST']))
{
show_error('Shove off hax0r!');
}
回答by sumanchalki
If you want to run cron job by running url, here is a great article
如果你想通过运行 url 来运行 cron 作业,这里有一篇很棒的文章
http://www.nbill.co.uk/documentation/setting-up-a-cronjob.html
http://www.nbill.co.uk/documentation/setting-up-a-cronjob.html
回答by Rahul Dadhich
Use php-cli instead of php
Ex:
使用 php-cli 代替 php
Ex:
/usr/bin/php-cli /home/CPANEL_USER/public_html/index.php cronJobs deleteNotifications

