CLI (Command Line Interface) Cron Job not getting correct controller

耗尽温柔 提交于 2019-12-08 09:31:05

问题


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.


回答1:


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

  1. Create cron.php config file in application/config with data as:

$config['CRON_TIME_LIMIT']=0;

$config['argv'] =array(1 => 'birthday');

$config['CRON_BETA_MODE']=false;

  1. 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";
?>
  1. Run cron the file as php

/home/dlp/public_html/abc.org/cron.php



来源:https://stackoverflow.com/questions/18460678/cli-command-line-interface-cron-job-not-getting-correct-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!