Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

A PHP script run from the command line writes nothing to the error log

Errorphp script.php prints nothing and error_log stays empty

Short 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.

  • Applies to PHP 8.3, PHP 8.4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"));'
  2. 2Match what you see, then jump to its fix

    1. 1The values you edited are not the ones the first-step command printsThe CLI loads a different php.ini than the web serverFix
    2. 2log_errors prints string(1) "0" or an empty valuelog_errors is offFix
    3. 3Errors 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. 4log_errors and error_log look right, but the file never appears or never growsThe log file cannot be written by the user running phpFix

What does the error look like?

  • php script.php prints nothing and error_log stays empty
  • Errors 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 --ini

2Fix

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.php

3Verify

The first-step command now shows the values you set.

php -r 'var_dump(ini_get("log_errors"));'

Confirmed by php.net

Did this fix it?

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_ALL

3Verify

Trigger a notice and look for it in the log.

php -r 'echo $undefined;'

Confirmed by php.net

Did this fix it?

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.log

3Verify

Run a script that fails and read the file.

tail -n 5 /var/log/php/cli-errors.log

Confirmed by php.net

Did this fix it?

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.log

2Fix

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.log

3Verify

Run a failing script again; the file grows.

php -r 'echo $undefined;' && tail -n 3 /var/log/php/cli-errors.log

Confirmed by php.net

Did this fix it?

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

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 fix

Back 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.