1

I am using a library which connects to a remote service. The library will generate a PHP Fatal error / Uncaught ErrorException in case the connection terminates unexpectedly.

My PHP script is running as a daemon (via Systemd), so I would like to automatically restart the daemon after a while to reconnect. All this is setup in Systemd, so if PHP exits with the status 4, the daemon will be restarted after some time.

PHP uses the exit code 255 by default for fatal errors, so I resolved to using a shutdown function similar to the following:

function shutdown() {
    // Do a bit of this and that (e.g. assert 
    // there actually was the relevant error)
    exit(4);
}

register_shutdown_function('shutdown');
trigger_error('Test', E_USER_ERROR);

But whatever I try, I cannot influence the exit code in any way. I have not found anything helpful in the documentation.

Is there any known way to manipulate the exit code within a shutdown function or by other means after the fatal error has already been generated?

1
  • related: how does one obtain the exit code/status from within the shutdown function? Is it possible? Commented Oct 5, 2017 at 20:59

1 Answer 1

2

This is a bug in PHP, which apparently has been filed multiple times over the years:

https://bugs.php.net/bug.php?id=65275

https://bugs.php.net/bug.php?id=62725

https://bugs.php.net/bug.php?id=62294

https://bugs.php.net/bug.php?id=23509

It is supposed to be fixed already, but as of PHP 5.5.30, I am still experiencing it myself.

I suppose you could try reverting to PHP 5.3, which was reportedly working as expected. Or you could check out one of the most recent releases (5.6.18 or 7.0.3 as of this answer), to see if the bug is indeed fixed.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I was already thinking about opening a bug report myself. Hadn't found those you linked yet, they're a good starting point.
Well, then I guess I won’t be holding my breath on a fix…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.