PHP errors & notices when executing a file from CLI

Posted July 5, 2016 5.5k views


After installing a PHP script (CPVlab) I am trying to set up a cronjob that executes a PHP script every day. The script is executed like this : php5 /var/www/htdocs/cron-wurfl.php

After testing this line from the command line, I got the following output :(if it needs t

PHP Warning: visualStylesCheckCall(license/license.php): failed to open stream: 
No such file or directory in /var/www/htdocs/lib/ on line 0

PHP Fatal error: visualStylesCheckCall(): Failed opening required 'license/license.php'
(include_path='.:/usr/share/php:/usr/share/pear') in /var/www/htdocs/lib/ on line 0

I was able to reduce these error messages by playing with the permission scheme (adding myself to the www-data group). However, that didn’t solve it all, as now, when I run : php5 /var/www/htdocs/cron-wurfl.php , I get the output below :

PHP Notice:  Undefined index: SERVER_NAME in /var/www/htdocs/lib/ on line 0
PHP Notice:  Undefined index: REQUEST_URI in /var/www/htdocs/lib/ on line 0

Could this still have to do with the permission scheme I am using ? I have myself as the owner of the htdocs directory, while it is also owned by the group of www-data. Myself as well as www-data (the user) are part of this group.

Anyone know how to solve it ?


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

This does not have to do with permissions. The script you are attempting to run was not written to be run from the command line and is relying on environment variables that are passed to it by the web server. Since the scripts are not being run by the web server process these values are not set. I would recommend editing the PHP code itself to hard-code these variables or adapt the script to read them from the command line.

  • Thanks for the info Ryan … What you are saying sounds logic. On the other side, the install instructions of the application show a cron job setup in cpanel (which of course I don’t have), so I my logic told me that the it should work from through cron tab as well … isn’t a cron job a cron job ? Would it make a difference if I try to execute the script as www-data ? Or is the environment set only for PHP-FPM (I’m using LEMP) -> thus causing php cli to be left out ?

    • That is odd. The most recent errors you received are referring to parts of the $SERVER[] variable. This is set by the web server (or FPM) process so they are not inherent to the user’s session. They include details like the requested URL, name of the server, remote user if there is one, etc.

      To the best of my knowledge, cPanel does use a standard cron setup behind it’s utilities so this is a bit of a mystery.

      • Yes… and what even more strange is that the first series of errors (including the FATAL error) all went away when I added myself to to the www-data group. Right now I even made the scheme www-data:www-data in order to be sure that the web servers covers everything …

        The notices about the environment went away after adding myself to the www-data group like I mentioned. And when I run the cron-wurfl.php file like so : php5 cron-wurfl.php , I can see in the CPVlab error log that the cron job started AND finished (2 seperate entries). However when I put that same line in a Crontab and wait till it executes I will only find an entry in the error log which says that the cronjob started … it doesn’t receive an entry that it’s finished…