Search code examples
php.htaccesserror-handlingcronstderr

Output a STDERR for PHP errors without changing php.ini


I have some PHP scripts that run every minute via CRON. I don't want to receive emails every time they run correctly but I do want to receive an email if they throw an error (e.g. if they use too much memory).

To run the CRON:

          • php myscript.php >/dev/null

This should send an email only when the result of stderr is not null, i.e. when there is a standard error.

However, PHP errors don't create a stderr, instead they are included in the stdout so no email is sent.

I have tried setting display_errors to stderr as shown in this question but it's a different use case as the scripts are not run by CRON. I have tried the following which have not worked:

including this in the script itself doesn't work because a fatal errors mean the script doesn't run:

ini_set ('display_errors', 'stderr');

including this in an .htaccess file (note PHP FPM is used) didn't work - I'm not sure why:

php_flag display_errors stderr

Changing the php.ini file is not an option because I don't want the change to affect the whole site, just a few files.

So my question is - how can the PHP errors be sent to stderr so that I can receive them via email?


Solution

  • The php executable takes command line options - you can either specify which php.ini it should use, or set specific config options via the -d parameter.

    https://www.php.net/manual/en/features.commandline.options.php