istepaniuk
istepaniuk

Reputation: 4269

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

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.

Upvotes: 4

Views: 343

Answers (1)

Joundill
Joundill

Reputation: 7584

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

Then use it in your handleShutdown function:

$exitCode = uopz_get_exit_status();

Upvotes: 3

Related Questions