Codeigniter cron 工作不起作用

Gia*_*dze 1 php codeigniter codeigniter-2

我有一个像这样的 cron 工作:

/usr/bin/php /var/www/website/public_html/index.php className methodName
Run Code Online (Sandbox Code Playgroud)

如果我在终端中运行它,它会运行,但不输出任何内容。如果我传递了错误的方法名称,它会成功运行。如果我传递错误的类名,它会输出一个网站 404 错误。

例如,我还有一个路由,它将“en”添加到 url 中

http://www.website.com/en/home/index
Run Code Online (Sandbox Code Playgroud)

这可能是问题吗?

我的 config.php 设置是:

$config['uri_protocol'] = 'AUTO';
$config['index_page'] = '';
Run Code Online (Sandbox Code Playgroud)

Vic*_*kel 5

通过CLI(命令行界面)为 cron-jobs 准备 CodeIgniter 2.x 的步骤:

第一:创建根index.php文件的副本并将其保存在根目录中cli.php

第二:在您的 cli.php 中替换<?php为以下代码:

#!/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) . '/';
Run Code Online (Sandbox Code Playgroud)

第三:像这样执行你的 cron 工作:

/usr/bin/php /var/www/website/public_html/cli.php controller method
Run Code Online (Sandbox Code Playgroud)

/var/www/website/public_html/您的服务器的主目录在哪里,您的index.phpcli.php.

笔记:

对于 CI 3.0,您可以在此处找到必要的信息

数据库:您需要在控制器方法中提供数据库配置设置,因为 cron 作业只是执行控制器的方法。所以它对任何数据库设置一无所知!

$config['hostname'] = "localhost";
$config['username'] = "username_admin";
$config['password'] = "password";
//etc..

$this->db  = $this->load->database($config, TRUE);
Run Code Online (Sandbox Code Playgroud)

调试:只需在您的 html 中添加一个链接来运行您的控制器方法,例如:(index.php/controller/method一旦您的网站上线,就删除它)

来源:非常有帮助