I'm trying to handle the very common event of somebody closing a running PHP CLI terminal window by clicking its "X" button 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 currently use register_shutdown_function()
, which sounds like it would be perfect for this. However, 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 is run when the script exited/ended on its own.
sapi_windows_set_ctrl_handler()
does execute when Ctrl + C is pressed, but not when the window is closed, which is by far the most important and common way my PHP CLI processes are "unexpectedly" closed.
The pcntl_*
functions cannot be used since they are Linux-only, while all my PHP code needs to be cross-platform.
Specs: PHP 7.4.10 on Windows 10 Pro 2004.