6

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

  1. 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 whatever taskkill /PID XXXX sends on the process side.
  2. It doesn't seem to work with commands in cmd.exe. I tried to run a simple server process in one cmd.exe, get its PID and in another window to taskkill /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?

samus
  • 6,102
  • 6
  • 31
  • 69
Honza Javorek
  • 8,566
  • 8
  • 47
  • 66

2 Answers2

4

GenerateConsoleCtrlEvent signals a console application as if the user had pressed Ctrl+C or Ctrl+Break. Console applications can ignore these signals (using SetContolCtrlHandler), but, by default, they just do an ExitProcess on themselves.

This is the same signal Windows uses when the user closes the console window, logs off, or shuts down.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • Do you know how can I send these signals from a programming language, such as JavaScript (node.js)? – Honza Javorek Feb 17 '17 at 18:34
  • @HonzaJavorek: Sorry, I do not. Maybe you should add an appropriate language tag to the question to get the folks who do know to notice the question. – Adrian McCarthy Feb 25 '17 at 00:36
4

TaskKill uses PostMessage(hwnd, WM_CLOSE, 0, 0); (basically the same as pressing the X on a window) if the process has a visible window.

If you know the application is a console application you can use GenerateConsoleCtrlEvent instead.

Anders
  • 97,548
  • 12
  • 110
  • 164