繁体   English   中英

Cron作业输出默认控制器

[英]Cron job outputting the default controller

问题

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

试过

我通过简单的回显将另一个文件添加到我的www目录test.php并正确运行,因此我确定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

有关启用查询字符串的更多信息: CodeIgniter 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和通过Web服务器的PHP使用不同的环境变量(特别是可以使用不同的php.ini文件)以及诸如重写和Web服务器的其他处理之类的事情可能会造成问题(特别是通过.htaccess,但有时也会在主配置中),这可能会引起问题。

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

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

您可能会做一个简单的测试,在其中使用CodeIgniter脚本来回显您想使用的控制器和方法。 我猜想您的命令行/ cron并没有正确传递变量,并且默认将其作为默认主页面。

我昨天了解到的是,您需要a将控制器的功能限制为仅控制台使用,从而在控制器构造函数中阻止其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 /用户名/www/executeCron.php --run = / controllername / methodname

在crontab中,您还可以使用curl来更好地运行URL

所以你可以这样

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

希望这个能对您有所帮助

我遇到了同样的问题。 我尝试了很多事情。 最后,为什么显示index.php的原因是因为我的config.php的以下行设置为“ QUERY_STRING”

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

$ config ['uri_protocol'] =“”自动“;

它按预期工作。

希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM