Friday, May 27, 2022

[SOLVED] CodeIgniter is_cli_request() returns false when run from cronjob

Issue

Here is my cronjob's command

php /home/toolacct/public_html/index.php cronjob fetch_emails

Here is my CodeIgniter cronjob controller

class Cronjob extends CI_Controller {
    function __construct() {
        parent::__construct();
        die($this->input->is_cli_request()?'T':'F');
        $this->load->model('cronjob_model');
    }   

    public function fetch_emails() {
        $this->cronjob_model->fetch_emails();
    }

    public function index_popularity() {
        $this->db->query('CALL update_email_data_popularity()');
    }
}

The cronjob says "F" Why?


A side note: If I create a function like this

function is_cli_request() {
    return isset($_SERVER['SHELL']);
}

It returns true or false correctly, while CodeIgniter always returns false.


Solution

The Input Class Documentation says that $this->input->is_cli_request() uses the STDIN constant to check if its running on the command line.

Try using the command line version of PHP instead (php-cli), since the STDIN constant doesn't appear to be set while running the standard PHP executable (php).



Answered By - Quetzy Garcia
Answer Checked By - David Marino (WPSolving Volunteer)