输出默认控制器的 cron 作业


Cron job outputting the default controller

PROBLEM

我正在为我的网站使用 CodeIgniter v2.1.4。我已经使用以下命令php -q /home/user_name/www/index.php controller my_method设置了一个每 2 小时运行一次的 cronjob。但是,这会输出我的默认控制器页面 html 内容(这是我主页的 html 内容)。

我在 www 目录中添加了另一个名为 test.php 的文件,其中包含一个简单的 echo 并且它运行正常,因此我确定问题存在于 CI 中。此外,当我使用浏览器访问我尝试通过 cron 作业执行的控制器/方法时,它会输出正确的消息。

所需解决方案

我按照另一个线程的建议使用了wget -q http://mywebsite.com/controller/my_method并且它工作正常,但我想使用php -q的方式,因为这样我将能够拒绝从浏览器直接访问我的脚本。

CodeIgniter 使用 CLI 请求。所以你需要使用 PHP CLI。

/usr/bin/php-cli  /home/user_directory/public_html/index.php controller method

如果您的服务器上没有 PHP CLI,还有另一种选择;

您可以在 CodeIgniter 上启用查询字符串并尝试像这样运行。

php -q /home/user_directory/public_html/index.php?c=controller&m=method

有关启用查询字符串的详细信息:代码点火器 URLS

它应该只是(来自cPanel):

php /home/user_name/www/index.php controller method

但是,如果您使用的是命令行并且输入了crontab -e

30 2 * * * php /home/username/index.php welcome show

上面的示例将每天凌晨 2:30 运行。

希望这有帮助!

我很

抱歉没有更好地了解CodeIgnitre,但我想提一下,命令行PHP和PHP通过Web服务器使用不同的环境变量(特别是可以使用不同的php.ini文件)以及重写和Web服务器可能执行的其他处理(特别是通过.htaccess,但有时也在主配置中)可能会导致这里的问题。

如果你在PHP脚本中运行phpinfo()函数,它会告诉你应该php.ini它正在使用的文件。

我假设您已经检查了您的控制器和方法,并确保它们的拼写准确且正确(我不知道大写是否算数)。

你可以做一个简单的测试,你使用你的CodeIgniter脚本来回显它认为你要求哪个控制器和方法。 我猜你的命令行/cron one 没有正确传递变量,它默认到主页作为回退。

我昨天学到的是,您需要将函数从控制器限制为仅控制台使用,在控制器构造函数上阻止其Web调用

class Hello extends CI_Controller {
    function __construct() {
        if (isset($_SERVER['REMOTE_ADDR'])) {
            die('Command Line Only!');
        }
        parent::__construct();
    }
    public function message($to = 'World'){
        echo "Hello {$to}!".PHP_EOL;
    }
}

然后,您需要在 ci 文件的根目录下创建一个cli.php命令行文件

if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Command Line Only!');
}
set_time_limit(0);
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = $argv[1];
require dirname(__FILE__) . '/index.php';

最后,为了运行该命令,您在控制台中键入以下内容:

php cli.php Hello message

php cli.php Hello message "parameter" 

输出将是"Hello World!""Hello parameter!",您可能需要对控制器和函数名称进行一些调整,更多信息在这里链接以及来自codeigniter的cli文档。

在项目的根目录下,创建一个包含以下内容的 executeCron.php 文件:

#!/usr/local/bin/php
<?php
/*
|--------------------------------------------------------------
| CRON JOB BOOTSTRAPPER
|--------------------------------------------------------------
| PURPOSE
| -------------------------------------------------------------
| This script is designed to enable CodeIgniter controllers and functions to be easily called from the command line on UNIX/Linux systems.
|
|
| SETUP
| -------------------------------------------------------------
| 1) Place this file somewhere outside your web server's document root
| 2) Set the CRON_CI_INDEX constant to the location of your CodeIgniter index.php file
| 3) Make this file executable (chmod a+x cron.php)
| 4) You can then use this file to call any controller function:
|    ./cron.php --run=/controller/method [--show-output] [--log-file=logfile] [--time-limit=N] [--server=http_server_name]
|
|
| OPTIONS
| -------------------------------------------------------------
|   /controller/method   Required   The controller and method you want to run.
|
| NOTE: Do not load any authentication or session libraries in controllers you want to run via cron. If you do, they probably won't run right.
*/
    define('CRON_CI_INDEX', dirname(__FILE__)."/index.php");   // Your CodeIgniter main index.php file
    define('CRON', TRUE);   // Test for this in your controllers if you only want them accessible via cron

# Parse the command line
    $script = array_shift($argv);
    $cmdline = implode(' ', $argv);
    $usage = "Usage: executeCron.php /login/index 'n'n";
    $required = array('--run' => FALSE);
    foreach($argv as $arg)
    {
       /*switch($arg)
        {
           case 'cronRemoveStories/index':*/
                // Simulate an HTTP request
                //$_SERVER['PATH_INFO'] = $arg;
                $_SERVER['REQUEST_URI'] = $arg;
                $_SERVER['REMOTE_ADDR'] = '';
                $required['--run'] = TRUE;
                break;
           /* default:
                die($usage);
        }*/
    }
    if(!defined('CRON_LOG')) define('CRON_LOG', 'cron.log');
    if(!defined('CRON_TIME_LIMIT')) define('CRON_TIME_LIMIT', 0);
    foreach($required as $arg => $present)
    {
        if(!$present) die($usage);
    }

# Set run time limit
    set_time_limit(CRON_TIME_LIMIT);

# Run CI and capture the output
    ob_start();
   if(file_exists(CRON_CI_INDEX)){
        require(CRON_CI_INDEX);           // Main CI index.php file
   }else{
        die(CRON_CI_INDEX." not found.");
   }
   $output = ob_get_contents();
   ob_end_clean();
# Log the results of this run
    error_log("### ".date('Y-m-d H:i:s')." cron.php $cmdline'n", 3, CRON_LOG);
?>

现在编写您的控制器并按如下方式配置 Crontab(如果需要,请更正您的文件路径):

0 */2 * * * php/home/user_name/www/executeCron.php --run=/controllername/methodname

在 crontab 中,您还可以使用 curl,它更适合运行 URL 的

所以你可以这样做

*/10 *  *  *  *     curl 'http://www.example.com/controller/action'

希望这对你有帮助

我遇到了同样的问题。我尝试了很多东西。最后,显示索引.php的原因是因为我的配置中的以下行.php设置为"QUERY_STRING"

当我将其更改为以下内容时

$config['uri_protocol']= "自动";

它按预期工作。

希望这有帮助