Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to know the exit status code from within a shutdown function?

Tags:

php

Is there a way to know the value passed to exit() from within a shutdown function?

For example:

<?php

function handleShutdown(){
    // Is there a way to know the value passed to exit() at this point?

    someCleanup();
}

register_shutdown_function('handleShutdown');

if(somethingWentWrong()){
    exit(3);
}

exit(0);

                                          

This is in the context of a complex set of legacy scripts running in CLI mode. I am looking for a solution to instrument their exit status within php that does not involve modifying all the points where the scripts invoke exit() (so, not using a global variable, define or forking the whole script).

There is this other SO question asking to know whether the exit was is "clean", but it does not address the exit code at all.

like image 233
istepaniuk Avatar asked Nov 02 '25 02:11

istepaniuk


1 Answers

You can install uopz, which will give you uopz_get_exit_status().

Then use it in your handleShutdown function:

$exitCode = uopz_get_exit_status();
like image 136
Joundill Avatar answered Nov 04 '25 18:11

Joundill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!