Is there some way to detect a process/window kill/close/end from PHP CLI on Windows?

[email protected] Sun, 12 Jul 2020 02:00:25 +0200 (CEST)
Newsgroups php.windows
Message-ID <[email protected]>
------=_Part_57403_1960067768.1594512025595
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

I'm trying to handle the very common event of somebody killing a running PHP CLI terminal window by clicking its "X" or by killing it from the command line with "taskkill".

By "handle", I mean "I want this quick but important function of mine to run before it ends".

I current already use `register_shutdown_function()`, which sounds like it would be perfect for this, only it's never called except if the script ended by itself and absolutely nothing unexpected happened.

In my tests, the function set by `register_shutdown_function()` is NOT called when the window is closed (or the process is killed by other means), nor when `Ctrl + C` is pressed while it's running. It *only* seems to run when the script exited on its own.

`sapi_windows_set_ctrl_handler()` *does* execute when Ctrl + C is pressed, but not when the window is closed, so it has very limited use. I guess I will set `sapi_windows_set_ctrl_handler()` to the same function as `register_shutdown_function()`, but that, of course, still leaves the most important situation when the PHP CLI window is "X'd down".

I almost can't believe they have *two* functions extremely related to this, but none for detecting the "kill signal"? In fact, why did they even bother to make `sapi_windows_set_ctrl_handler()` a separate function instead of just baking it into `register_shutdown_function()`?

I just want to be able to detect the "close window" signal. Please let there be a way... I really need this. It's actually very strange to me that the `sapi_windows_set_ctrl_handler()` function was just recently added, and I don't understand why some things are Windows-specific when one of the major great things about PHP is that it's (supposedly) cross-platform.

PHP 7.4.8
Windows 10 Pro 1903

------=_Part_57403_1960067768.1594512025595--