如何通过CRON运行CodeIgniter文件? [英] How to run a CodeIgniter file through CRON?
问题描述
我过去尝试过以下方法:
<?php
set_time_limit ;
$ _SERVER ['PATH_INFO'] ='cron / controller / index';
$ _SERVER ['REQUEST_URI'] ='cron / controller / index';
require_once('index.php');
?>
并将其放在codeigniter安装目录中的一个文件中,调用cron.php,然后调用it via:
php /home/[username]/public_html/my_project/cron.php
如果我在我的浏览器中输入cron.php的URL,它的工作原理,但是每当它通过CRON运行我得到404错误。将以下代码放在CodeIgniter的 show_404()
函数中
show_404($ page ='')
{
print_r($ _ SERVER);
echo\\\
\\\
;
die($ page);
}
会将以下输出通过电子邮件发送给我:
数组
/ pre>
(
[SHELL] => / bin / sh
[MAILTO] => me @ gmail.com
[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]
这里我用[我]代替我的实际用户名。
/ p>
解决方案通过CodeIgniter运行cron的最简单的方法是通过应用程序提供cron URL。
然后通过wget调用它
wget -O - -q -t 1 http: /www.example.com/cron/run
在控制器中,您可以使用日志确保
第二种方法是使用lynx
/ usr / local / bin / lynx -source http://www.example.com/cron/run
pre>
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:
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 CodeIgniterfunction 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] => me@gmail.com [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.
Any ideas?
解决方案The simplest way to run a cron via CodeIgniter is to make a cron URL available via your app.
Then call it via 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.
A second method would be to use lynx
/usr/local/bin/lynx -source http://www.example.com/cron/run
这篇关于如何通过CRON运行CodeIgniter文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!