I'm creating a command-line application, which spawns a process (command defined by a user, usually an HTTP server) and when the application's job is done, I want to let the process know it should terminate.
In UNIX, I can do that by sending SIGTERM and if the process doesn't end, then I can kill it brutally by SIGKILL.
In Windows, I struggle to find an alternative to the SIGTERM scenario. I learned there's taskkill /PID XXXX
(without /f
!), but
- I found no information about what
taskkill /PID XXXX
does under the hood, hence I can't test it. I can't find how to handle whatevertaskkill /PID XXXX
sends on the process side. - It doesn't seem to work with commands in
cmd.exe
. I tried to run a simple server process in onecmd.exe
, get its PID and in another window totaskkill /PID XXXX
it, but taskkill refused to do that:ERROR: The process with PID XXXX could not be terminated. Reason: This process can only be terminated forcefully (with /F option).
So my question is: How to inform a command-line process in Windows that it should terminate without forcefully terminating it? How to receive and act upon such message on the process-to-be-terminated side?