Closing a Windows terminal at process startup

In the command prompt window, I have a working process. While the process is still running, I press (red) "X" in the corner of the command window. The command window closes and the running process ends.

On Linux, closing the parent terminal of the running process will send this SIGHUP process. How can I catch this event on Windows?

+2
source share
3 answers

The SIGHUP equivalent is provided through a callback that you register with SetConsoleCtrlHandler . The callback function will be called on an arbitrary threadpool thread with dwCtrlType = CTRL_CLOSE_EVENT. You have 5 seconds to clear, you cannot cancel the close.

Sample code is available in this MSDN article.

+5
source

This cannot be done from the command line. You will need something secondary (vbs, powershell or custom MyApp.exe) to catch cmd.exe when it is closed and respond accordingly.

For example, a VBS WMI monitor.

 strComputer = "." Set objSWbemServices = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & "\\" & strComputer & "\root\cimv2") Set objEventSource = objSWbemServices.ExecNotificationQuery( "SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name = 'cmd.exe'") Set objEventObject = objEventSource.NextEvent() Wscript.Echo "CMD.exe closed" 
0
source

"SIGHUP" ... tested on Windows 8.1 pro + Node.js 0.10.24 works fine.

 process.on('SIGHUP', function(){ //Your code goes here. }); 
0
source

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


All Articles