Gracefully stop using the command line in Windows

I am creating a command line application that spawns a process (a user-defined command, usually an HTTP server), and when the application’s job is complete, I want the process to know that it should complete.

On UNIX, I can do this by sending SIGTERM, and if the process does not end, I can brutally kill it SIGKILL.

On Windows, I struggle to find an alternative to the SIGTERM script. I found out there taskkill /PID XXXX(without /f!), But

  • I did not find information about what is doing taskkill /PID XXXXunder the hood, so I can not test it. I can not find how to handle any messages taskkill /PID XXXXon the process side.
  • It does not work with teams in cmd.exe. I tried to start a simple server process in one cmd.exe, get its PID in another window taskkill /PID XXXX, but taskkill refused to do this: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 do I tell the command line process on Windows that it should terminate without being forced to terminate? How to receive and process such a message on the side that should be completed?

+4
source share
2 answers

GenerateConsoleCtrlEvent , Ctrl + C Ctrl + Break. ( SetContolCtrlHandler), ExitProcess.

, Windows, , .

+3

TaskKill PostMessage(hwnd, WM_CLOSE, 0, 0); ( , X ), .

, , GenerateConsoleCtrlEvent .

+3

Source: https://habr.com/ru/post/1670104/


All Articles