CLI(命令行界面)Cron作业未获得正确的控制器 [英] CLI (Command Line Interface) Cron Job not getting correct controller
问题描述
我在服务器上几乎成功设置了一个Cron作业,但是我无法调用正确的控制器。
//确保请求是由CRON作业完成的
if(! $ this-> input-> is_cli_request())exit('Only CLI access allowed');
我的输出是由Cron守护进程通过电子邮件发送的。我已尝试此命令,以下是我的结果。
工作:
/ usr / bin / php /home/dlp/public_html/abc.org/index.php birthday
结果:
我在默认控制器的第一封电子邮件HTML输出中收到两封电子邮件 index.php
和生日
控制器的第二封电子邮件输出。
<?php
defined('BASEPATH')OR exit ('不允许直接脚本访问);
class Birthday extends CI_Controller {
function __construct()
{
parent :: __ construct();
$ this-> load-> helper('url');
$ this-> load-> database();
$ this-> load-> library('email');
}
/ ****在生日上发送新闻信函数**** /
function index()
{
error_log(birthday function call,1,abc@gmail.com);
exit;
}
}
?>
我不知道我做错了什么。
先感谢您的帮助。
您在cronjob中只写控制器名称,也当方法是索引时。所以在你的情况下,你在你的cronjob写 birthday / index
。
OR
- 在<$ c中创建
cron.php
配置文件$ c> application / config ,其中数据为:
$ config [ 'CRON_TIME_LIMIT'] = 0;
$ config ['argv'] = array(1 =>'birthday');
$ config ['CRON_BETA_MODE'] = false;
- 在根parrallel处创建cron.php到包含数据的应用程序文件夹
-
//!/ usr / bin / php
<?php
define('CRON',TRUE);
//加载CRON配置
require('/ home / dlp / public_html / abc.org / application / config / cron.php');
//设置CRON模式(live或beta)
define('CRON_BETA_MODE',$ config ['CRON_BETA_MODE']);
//设置index.php位置
if(isset($ config ['CRON_CI_INDEX'])&& $ config ['CRON_CI_INDEX'])
define CRON_CI_INDEX',$ config ['CRON_CI_INDEX']);
else
define('CRON_CI_INDEX','/home/dlp/public_html/abc.org/index.php');
if(count($ argv)< 2)
if(count($ config ['argv'])){
$ argv = array_merge($ argv,$ config ['argv']);
$ _SERVER ['argv'] = $ argv;
} else
die('Use:php cron.php controller / method');
//模拟HTTP请求
$ _SERVER ['PATH_INFO'] = $ argv [1];
$ _SERVER ['REQUEST_URI'] = $ argv [1];
// $ _ SERVER ['SERVER_NAME'] = $ config ['SERVER_NAME'];
//设置运行时限制
set_time_limit($ config ['CRON_TIME_LIMIT']);
//运行CI并捕获输出
ob_start();
chdir(dirname(CRON_CI_INDEX));
// echo==.CRON_CI_INDEX;死;
require(CRON_CI_INDEX); // main CI index.php file
$ output = ob_get_contents();
if(CRON_FLUSH_BUFFERS === TRUE)
while(@ob_end_flush()); //显示缓冲区内容
else
ob_end_clean();
echo\\\
;
?>
- 将cron作为php
/home/dlp/public_html/abc.org/cron.php
I have almost successfully setup a Cron job on my server, but I cannot call the correct controller.
When I remove the CLI only if statement I can successfully run the script from my browser.
// Make sure the request is being made by a CRON Job
if ( ! $this->input->is_cli_request()) exit('Only CLI access allowed');
I am having the output being emailed by the Cron Daemon. I have tried this command and following is my results.
job :
/usr/bin/php /home/dlp/public_html/abc.org/index.php birthday
Result :
I get the 2 emails in 1st email HTML output of the default controller index.php
and in 2nd email output of birthdady
controller.
code of my controller is.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Birthday extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
$this->load->library('email');
}
/**** function for sending news letter on birthday ****/
function index()
{
error_log("birthday function call",1,"abc@gmail.com");
exit;
}
}
?>
I am not sure what I am doing wrong.
Thanks in advance for the help.
You are writing only controller name in cronjob but you should write method name also when the method is index. So in your case you write birthday/index
in your cronjob.
OR
- Create
cron.php
config file inapplication/config
with data as:
$config['CRON_TIME_LIMIT']=0;
$config['argv'] =array(1 => 'birthday');
$config['CRON_BETA_MODE']=false;
- Create cron.php at root parrallel to application folder containing data as
//!/usr/bin/php
<?php
define('CRON', TRUE);
// Load CRON config
require('/home/dlp/public_html/abc.org/application/config/cron.php');
// Set CRON mode ( live or beta )
define('CRON_BETA_MODE', $config['CRON_BETA_MODE']);
// Set index.php location
if (isset($config['CRON_CI_INDEX']) && $config['CRON_CI_INDEX'])
define('CRON_CI_INDEX', $config['CRON_CI_INDEX']);
else
define('CRON_CI_INDEX', '/home/dlp/public_html/abc.org/index.php');
if (count($argv) < 2)
if (count($config['argv'])) {
$argv = array_merge($argv, $config['argv']);
$_SERVER['argv'] = $argv;
} else
die('Use: php cron.php controller/method');
// Simulate an HTTP request
$_SERVER['PATH_INFO'] = $argv[1];
$_SERVER['REQUEST_URI'] = $argv[1];
//$_SERVER['SERVER_NAME'] = $config['SERVER_NAME'];
// Set run time limit
set_time_limit($config['CRON_TIME_LIMIT']);
// Run CI and capture the output
ob_start();
chdir(dirname(CRON_CI_INDEX));
// echo "== ".CRON_CI_INDEX; die;
require( CRON_CI_INDEX ); // main CI index.php file
$output = ob_get_contents();
if (CRON_FLUSH_BUFFERS === TRUE)
while (@ob_end_flush()); // display buffer contents
else
ob_end_clean();
echo "\n";
?>
- Run cron the file as php
/home/dlp/public_html/abc.org/cron.php
这篇关于CLI(命令行界面)Cron作业未获得正确的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!