0

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?

Ryan
  • 152
  • 1
  • 13

1 Answers1

1

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

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • Thanks for answering - does this mean I can add a php.ini file specifically for the scripts that I want it to affect? – Ryan Aug 03 '20 at 14:05
  • 1
    Yes, with `php -c /path/to.php.ini` you can specify, which php.ini it should use, to execute the specified script file. If you don’t need to change anything but where the error output is send, then `php -d display_errors=stderr` sould also do the trick here. – CBroe Aug 03 '20 at 14:18
  • I tried running `php -d display_errors=stderr my_script.php >/dev/null` and it still didn't work. – Matthew Knill May 15 '22 at 03:04