【问题标题】:Run cron job in CodeIgniter and Hostgator在 CodeIgniter 和 Hostgator 中运行 cron 作业
【发布时间】:2013-03-23 23:46:26
【问题描述】:

我正在尝试在 codeigniter 和 hostgator 中运行 follow cron 作业

/usr/bin/php /home/username/public_html/index.php cronjob contact martin

但没有任何反应,我收到以下电子邮件:

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined index:  REMOTE_ADDR</p>
<p>Filename: core/Input.php</p>
<p>Line Number: 351</p>

<p>Severity: Warning</p>
<p>Message:  Cannot modify header information - headers already sent by (output started at /home/iglesias/public_html/mysite.com/system/core/Exceptions.php:185)</p>
<p>Filename: libraries/Session.php</p>
<p>Line Number: 675</p>

我的控制器如下(只是发邮件测试)。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cronjob extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->cron();
}

function cron()
{
    if(!$this->input->is_cli_request())
    {               
        die();
    }
}

function contact($name)
{
    $this->load->library('email');
    $config['mailtype'] = 'html';
    $this->email->initialize($config);

    $this->email->from('test@gmail.com', $name);
    $this->email->to('test2@gmail.com');
    $this->email->subject('test');
    $this->email->message('test sent');             
    $this->email->send();
}
}

我做错了吗?我将衷心感谢您的帮助。

我更改了.htaccess 以在从URL 调用时删除index.php,我不知道这是否有什么关系。

谢谢

【问题讨论】:

  • 我只是想确认您能够使用 CLI 从 cron 作业运行 CodeIgniter 页面。您是如何收到包含错误消息的电子邮件的?

标签: php codeigniter cron command-line-interface


【解决方案1】:

@mariek 的精彩回答

但我们也可以通过在语法前使用@ 符号来做到这一点。

所以只是在“core/Input.php”第 352 行(CI 版本 2.2.2)中做了一点改动

$this-&gt;ip_address = $_SERVER['REMOTE_ADDR'];

$this-&gt;ip_address = @$_SERVER['REMOTE_ADDR'];

完成了。

【讨论】:

    【解决方案2】:

    我的答案是很久以后才出现的,但它可以帮助其他人。 这个错误显然是众所周知的,但只有一个快速修复它:http://ellislab.com/forums/viewthread/227672/ 它说当您自动加载会话库时会发生这种情况,它会调用未初始化的 CI_Input::ip_address() 或类似的东西。 快速修复在输入类中(core/Input.php Line 351 (CI 2.1.2)):

    $this->ip_address = $_SERVER['REMOTE_ADDR'];
    

    成为:

    if(isset($_SERVER['REMOTE_ADDR']))
       $this->ip_address = $_SERVER['REMOTE_ADDR'];
    else
       $this->ip_address = '0.0.0.0';
    

    希望这会有所帮助。

    【讨论】:

    • 只是为了确认 2.1.4 版本仍然是这种情况。并且修复有效!
    • 确实如此。 CI改变了整个论坛,我再也找不到帖子了。这就是为什么我将快速修复直接放在我的帖子中,这样即使源不可用也不会丢失。
    • 嗯,这是关于 GitHub 上的错误的完整帖子:github.com/bcit-ci/CodeIgniter/issues/1890
    【解决方案3】:

    检查 Hostgator 是否将 Apache 作为 Fast-CGI 而不是模块运行。如果是这种情况,PHP 的运行路径可能与您使用的不同。

    如果你查看phpinfo,你可能会看到相关信息。

    【讨论】:

      【解决方案4】:

      您是否在 config/routes.php 中设置了正确的路由?尝试从您的浏览器运行脚本(或注释掉执行部分)以确保它可以通过您当前的路由/htaccess 设置访问。

      【讨论】:

      • 我已经从浏览器运行了脚本,运行正常。
      • 我用 wget 和 url 修复了它
      • 请分享设置路由和 htaccess 设置的示例。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 2011-11-11
      • 2018-12-09
      相关资源
      最近更新 更多