A PHP script run from the command line writes nothing to the error log
php script.php prints nothing and error_log stays emptyShort answer
Logging is controlled by log_errors, which is off by default, and the CLI reads its own php.ini, not the one your web server uses. Run php --ini to see which file is loaded, switch log_errors on there, and set error_log to a path the running user can write. Without error_log, CLI errors go to stderr.
Fix it fast
1Find out which cause you have
Print the settings the CLI really uses, and the file they came from.
php --ini php -r 'var_dump(ini_get("log_errors"), ini_get("error_log"), ini_get("display_errors"), ini_get("error_reporting"));'2Match what you see, then jump to its fix
- 1
The values you edited are not the ones the first-step command printsThe CLI loads a different php.ini than the web serverFix - 2
log_errors prints string(1) "0" or an empty valuelog_errors is offFix - 3
Errors appear in the terminal only, and are lost under cron because stderr is not capturederror_log is unset, so messages go to stderr instead of a fileFix - 4
log_errors and error_log look right, but the file never appears or never growsThe log file cannot be written by the user running phpFix
- 1
What does the error look like?
php script.php prints nothing and error_log stays emptyErrors show in the browser log, but the same script run from cron or the terminal logs nothing
Cause 1 of 4
The CLI loads a different php.ini than the web server
1Check
PHP reads php.ini on every CLI run. In each search location it looks for a SAPI-specific file before php.ini, and php --ini lists the file and the extra scanned .ini files. A web SAPI keeps its own copy, so changing it does nothing for php on the command line.
php --ini2Fix
Edit the file that php --ini reports as loaded for the CLI, or point one run at a chosen file with -c.
php -c /etc/php/8.3/cli/php.ini script.php3Verify
The first-step command now shows the values you set.
php -r 'var_dump(ini_get("log_errors"));'Confirmed by php.net
Cause 2 of 4
log_errors is off
1Check
The manual lists log_errors with a default of 0. Production-style ini files also turn it off or leave it unset.
php -r 'var_dump(ini_get("log_errors"));'2Fix
Turn it on, and report all errors.
; php.ini (CLI)
log_errors = On
error_reporting = E_ALL3Verify
Trigger a notice and look for it in the log.
php -r 'echo $undefined;'Confirmed by php.net
Cause 3 of 4
error_log is unset, so messages go to stderr instead of a file
1Check
When error_log is not set, errors go to the SAPI error logger, which is stderr in the CLI.
php -r 'var_dump(ini_get("error_log"));'2Fix
Set error_log to a file, or redirect stderr when you start the script.
; php.ini (CLI)
error_log = /var/log/php/cli-errors.log
# or for one job
php script.php 2>> /var/log/php/cli-errors.log3Verify
Run a script that fails and read the file.
tail -n 5 /var/log/php/cli-errors.logConfirmed by php.net
Cause 4 of 4
The log file cannot be written by the user running php
1Check
The manual says the log file should be writable by the user that runs the script. Cron, a deploy user and root often differ.
id
ls -ld /var/log/php /var/log/php/cli-errors.log2Fix
Create the folder and file for the user that runs the job, or choose a path that user can write.
sudo mkdir -p /var/log/php
sudo touch /var/log/php/cli-errors.log
sudo chown "$USER" /var/log/php/cli-errors.log3Verify
Run a failing script again; the file grows.
php -r 'echo $undefined;' && tail -n 3 /var/log/php/cli-errors.logConfirmed by php.net
How do I stop it coming back?
- Keep log_errors on and error_log set in the CLI ini as well as in the FPM or Apache ini.
- Give cron jobs their own log path and redirect their stderr to it.
- Run php --ini as the same user that runs the job, since cron sees a different environment than your shell.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
PHP-FPM runs, but no PHP errors show up in any log file
A script fails with HTTP 500 and the error log has no matching line
PHP-FPM only logs script errors when log_errors is on, and it needs a log file the pool user can write.
4 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack end
CodeIgniter 4 cannot write its cache files
CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/
CodeIgniter 4's file cache needs a cache directory that the web server can really write to.
2 causes, each with a fixBack end
CodeIgniter 3 loads the model but the property is undefined
The model name you are loading is the name of a resource that is already being used: user
CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.
2 causes, each with a fix