Menu Close

How to solve PHP-CLI not logging error or showing output

Sometimes you will want to execute a long running process in the background because don’t want your user to wait it finished in the frontend. There are a few options to choose, and I chose executing the script in shell_exec.


Basically what I was trying to do is like


shell_exec('php bg.php ' . escapeshellarg($param) . ' > /dev/null &');

but somehow the script not executing and there is no error.

In bg.php, I have set it to log errors

ini_set('error_log', './error_log');

and still no error or output.

After a bit digging, I found out that bg.php got a syntax error. I tested it by running

$ php -l bot_bg.php
PHP Parse error:  syntax error, unexpected '}' in bg.php on line 11
Errors parsing bg.php

PHP couldn’t parse the file, thus using php.inidefault value instead of what I set in the code.



Well, the solution is quite simple. I simply set php.ini value that I want to override using -d flag


shell_exec('php -d error_log=errors.log -d error_reporting=E_ALL bg.php ' . escapeshellarg($param) . ' > /dev/null &');

Now PHP-CLI will use php.ini values that we set using -d flag instead of what default, even if the file couldn’t be parsed.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x