douxu2467 2016-03-23 18:41
浏览 46
已采纳

Codeigniter cron工作不起作用

I have a cron job like this:

/usr/bin/php /var/www/website/public_html/index.php className methodName

If I run it in terminal it runs, but outputs nothing. If I pass a wrong method name it runs successfully. If I pass wrong class name it outputs a website 404 error.

I also have a routing which adds "en" into url, for example

http://www.website.com/en/home/index

could this be the problem?

my settings of config.php are:

$config['uri_protocol'] = 'AUTO';
$config['index_page'] = '';
  • 写回答

4条回答 默认 最新

  • duanche9384 2016-03-23 22:41
    关注

    the steps to prepare CodeIgniter 2.x for cron-jobs via CLI (command line interface):

    1st: create a copy of your root index.php file and save it in your root as cli.php

    2nd: in your cli.php replace <?php with this code:

    #!/usr/local/bin/php
    <?php
    
    /* override normal limitations */
    set_time_limit(0);
    ini_set('memory_limit', '256M');
    
    /* deny direct call from web browser */
    if (isset($_SERVER['REMOTE_ADDR'])) die('Permission denied.');
    
    /* constants */
    define('CMD', 1);
    
    /* manually set the URI path based on command line arguments... */
    unset($argv[0]); /* except the first one */
    $_SERVER['QUERY_STRING'] =  $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = '/' . implode('/', $argv) . '/';
    

    3rd: execute your cron job like this:

    /usr/bin/php /var/www/website/public_html/cli.php controller method
    

    where /var/www/website/public_html/ is your server's home directory, the location of your index.php and cli.php.

    notes:

    for CI 3.0 you find the necessary information here

    database: you'll need to provide your db config settings in your controller method, as the cron job just executes the controller's method. So it doesn't know anything about any database settings!

    $config['hostname'] = "localhost";
    $config['username'] = "username_admin";
    $config['password'] = "password";
    //etc..
    
    $this->db  = $this->load->database($config, TRUE);
    

    debug: just add a link in your html to run your controller's method like: index.php/controller/method (remove that once you website is live)

    source: very helpful

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题