如果CLI会话在PHP中结束,有没有办法执行代码?

I'm having a small problem

Is there a way to execute a part of the code if a PHP script which is running on CLI is ended? Using CTRL+C or closing the command line window itself.

The script would still be running before closing it immediately

I thought of the __destruct method but I'm not really using a class

Any thoughts are appreciated.

You could have a look at register_shutdown_function

http://www.php.net/manual/en/function.register-shutdown-function.php

From the manual it says Note: Shutdown functions will not be executed if the process is killed with a SIGTERM or SIGKILL signal. While you cannot intercept a SIGKILL, you can use pcntl_signal() to install a handler for a SIGTERM which uses exit() to end cleanly.

If register_shutdown_function does not work then you will most likely be able to do something with http://www.php.net/manual/en/function.pcntl-signal.php