Codeigniter cron 工作不起作用 [英] Codeigniter cron job not working

查看:33
本文介绍了Codeigniter cron 工作不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的 cron 工作:

I have a cron job like this:

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

如果我在终端中运行它,它会运行,但什么也不输出.如果我传递了错误的方法名称,它会成功运行.如果我传递错误的类名,它会输出网站 404 错误.

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.

我也有一个路由,它在 url 中添加了en",例如

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

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

这可能是问题吗?

我的 config.php 设置是:

my settings of config.php are:

$config['uri_protocol'] = 'AUTO';
$config['index_page'] = '';

推荐答案

通过 CLI(命令行界面):

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

第一个:创建根 index.php 文件的副本,并将其作为 cli.php

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

2nd: 在您的 cli.php 中将 <?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) . '/';

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

3rd: execute your cron job like this:

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

其中 /var/www/website/public_html/ 是您服务器的主目录,您的 index.phpcli.php 的位置>.

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

注意事项:

对于 CI 3.0,您可以在此处

for CI 3.0 you find the necessary information here

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

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);

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

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)

来源:非常有帮助

这篇关于Codeigniter cron 工作不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆