# Getting notification that a subprocess is killed on Windows

**URL:** <https://www.ruby-forum.com/t/getting-notification-that-a-subprocess-is-killed-on-windows/194507>\
**Category:** Ruby\
**Created:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/getting-notification-that-a-subprocess-is-killed-on-windows/194507 "2010-09-06T16:39:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/getting-notification-that-a-subprocess-is-killed-on-windows/194507/1 "2010-09-06T16:39:52Z")

</div>

What is the proper way, under Windows, to detect that a process was  
killed? Crashed?

```
  Signal.list.each_key do |key|
    puts key
    Signal.trap(key, lambda {|signo| puts "#{key}: pid #{$$} with 

```

signo #{signo}"})  
end  
@the\_command\_pipe = IO.popen(the\_command)  
@the\_command\_pid = @the\_command\_pipe.pid  
Process.waitpid(@the\_command\_pid)  
@child\_process\_return\_status = $?  
.  
.  
.

```
Process.kill(:KILL, @the_command_pid) # Seems to properly kill my 

```

process

All that is TRAP’d is EXIT. Worse,  
@child\_process\_return\_status.exitstatus is zero.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [September 24, 2010, 2:35am UTC](https://www.ruby-forum.com/t/getting-notification-that-a-subprocess-is-killed-on-windows/194507/2 "2010-09-24T02:35:37Z")

</div>

On Fri, 3 Sep 2010 11:07:14 -0500, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote in [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> [Note: parts of this message were removed to make it a legal post.]
> 
> What is the proper way, under Windows, to detect that a process was killed? Crashed?

[snip code]

> All that is TRAP’d is EXIT. Worse, @child\_process\_return\_status.exitstatus is zero.

To get any level of detail for something like this, you’re going to  
have to use the Windows API directly or perhaps via the .NET stack  
(using IronRuby).

The following article contains code for doing this using C++ and the  
Win32 API. Note that it implements a process monitor, which may be  
more than you’re trying to do.

[http://www.codeproject.com/KB/threads/procmon.aspx](http://www.codeproject.com/KB/threads/procmon.aspx)

If you just want to monitor a single process, have a look at the API  
function WaitForSingleObject  
[http://msdn.microsoft.com/en-us/library/ms687032(VS.85).aspx](http://msdn.microsoft.com/en-us/library/ms687032%28VS.85%29.aspx).

Also, check this article for the function named TerminateApp() (ignore  
the stuff with “16” in the name - it’s for 16/32 bit versions of  
Windows like Windows95). [http://support.microsoft.com/kb/178893](http://support.microsoft.com/kb/178893).

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [September 24, 2010, 10:31am UTC](https://www.ruby-forum.com/t/getting-notification-that-a-subprocess-is-killed-on-windows/194507/3 "2010-09-24T10:31:27Z")

</div>

On 3 September 2010 17:07, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is the proper way, under Windows, to detect that a process was killed? Â Crashed?

Have you had a look at win32-process, one of the Win32Utils?

[http://win32utils.rubyforge.org/](http://win32utils.rubyforge.org/)
