Codeigniter “require(./vendor/autoload.php): failed to open stream” error just if it is run by cronjob - not a composer problem apparently

狂风中的少年 提交于 2020-03-05 03:04:26

问题


This is a consequent question of another one that can be found at Not working shared hosting cPanel cronjob under Codeigniter and it is worth to be asked apart. This is the context:

  1. codeigniter framework is used
  2. scheduler is defined as a controller and called by a cronjob
  3. the web server is a shared hosting managed with cPanel
  4. the cronjobs work properly if defined as pure PHP ones outside codeigniter
  5. the schedule controller if is called by the browser directly work fine: https://pharmacomparison.com/adminPanel/Hello/message/federico
  6. the same scheduler if lunched with the proper cli line by the cronjobs fails
    • running line in cronjob: /usr/local/bin/php public_html/adminPanel/index.php Hello message "federico"
    • resulting error: Warning: require(./vendor/autoload.php): failed to open stream: No such file or directory in /home/phar263d/public_html/adminPanel/index.php on line 66

Note that: the reference to the "./vendor/autoload.php" required file work properly when the scheduler is called by direct URL, but the same fails when the controller is called by the cronjob. Same controller, same index.php, same project. Any ideas? Thanks.

PS just as a reference here the simply testing code

<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');
require 'DashboardBase.php';

class Hello extends CI_Controller {

        public function message($to = 'World')
        {
                echo "Hello {$to}!".PHP_EOL;
                print "Hello {$to}!".PHP_EOL;
                log_message('error',"Hello {$to}!".PHP_EOL);
        }
}

?>

UPDATE

I have tried to force to an absolute path and actually that problem goes away; both many other paths in the CI system and vendors' files appeared as following

Notice: Undefined index: log_path in /home/phar263d/public_html/adminPanel/system/core/Log.php on line 127

Notice: Undefined index: log_path in /home/phar263d/public_html/adminPanel/system/core/Log.php on line 127

Warning: mkdir(): Invalid path in /home/phar263d/public_html/adminPanel/system/core/Log.php on line 131

Notice: Undefined index: log_threshold in /home/phar263d/public_html/adminPanel/system/core/Log.php on line 138

Notice: Undefined index: log_threshold in /home/phar263d/public_html/adminPanel/system/core/Log.php on line 142

A PHP Error was encountered

Severity:    Notice
Message:     Undefined index: HTTP_HOST
Filename:    /home/phar263d/public_html/adminPanel/application/config/config.php
Line Number: 41

Backtrace:
        File: /home/phar263d/public_html/adminPanel/application/config/config.php
        Line: 41
        Function: _error_handler

        File: /home/phar263d/public_html/adminPanel/index.php
        Line: 318
        Function: require_once


Hello federico!
real path /home/phar263d/public_html/adminPanel/vendor/autoload.phpHello federico!

It means that when the scheduler php script called by CLI/cronjobs has a completely different environment and base folders reference with respects when it is run by the WEB server. Any idea how to run the PHP scheduler by CLI having the same reference environment of the WEB server?

WORKAROUND

I found a workaround, but it has a drawback, so it is not the solution. The workaround is this: define a calling script outside the Codeigniter space that is called by the cronjob. The script then requests the URL of the Codeigniter scheduler. Here the simple code of the script run by the cronjob:

<?php


  $payload = file_get_contents('https://pharmacomparison.com/adminPanel/Hello/message/federico');

  echo $payload;
  return $payload;
?>

The problem is that in this way the scheduler must be reachable from the extern otherwise also the cronjob script couldn't access it; this obviously is a breach for safety. So what it is needed is a way to keep the WEB environment and all the variables and constants when calling from CLI; I found strange there is no mention about it even in the Codeigniter manual. Any ideas?

来源:https://stackoverflow.com/questions/59703413/codeigniter-require-vendor-autoload-php-failed-to-open-stream-error-just-i

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