Killing a Process started with Kernel.system

  • Thread starter Martin aus Chemnitz
  • Start date
M

Martin aus Chemnitz

Hi!

I know, this question was asked a dozen times, but I can't believe that
Ruby does not offer a suitable solution. I therefore write this posting
more in the spirit of an urgent feature request.

Under Windows, I want to start an external program and kill it on
exitting my Ruby script.

begin
$t = Thread.new { system("everlasting") }
...
ensure
$t.kill
end

does not stop the everlasting program, it stops $t, not its child
processes. Such a command is urgently needed!

Yukihiro Matsumoto writes in
If you want to kill child processes when the thread is down,

th = Thread.new{
Thread.current[:children] = []
begin
... add child processes to Thread.current[:children]
ensure
Process.kill("TERM", *Thread.current[:children])
end
}
but I think I can't get the PID of a system call either.

I would be glad if such a feature could be implemented. (Or, of course,
if anyone can tell me suitable solution.)

Martin
 
B

Bill Kelly

From: "Martin aus Chemnitz said:
Under Windows, I want to start an external program and kill it on
exitting my Ruby script.

begin
$t = Thread.new { system("everlasting") }
...
ensure
$t.kill
end

does not stop the everlasting program, it stops $t, not its child
processes. Such a command is urgently needed!

Yukihiro Matsumoto writes in
If you want to kill child processes when the thread is down,

th = Thread.new{
Thread.current[:children] = []
begin
... add child processes to Thread.current[:children]
ensure
Process.kill("TERM", *Thread.current[:children])
end
}
but I think I can't get the PID of a system call either.

I would be glad if such a feature could be implemented. (Or, of course,
if anyone can tell me suitable solution.)

I'm guessing the problem is your "everlasting" program
spawns itself in the manner of rubyw.exe, where it
does sort of the windows equivalent of daemonizing
itself. So your system("everlasting") call returns
immediately, and your "everlasting.exe" is launching
as a separate process, for which you have no way (that
I know of) of obtaining the PID.

Maybe a windows guru can help. I'm doubtful this is
a behavior ruby has any control over. If I understand
correctly, you may be up against a fundamental problem
with Windows' way of launching non-console applications.
(I guess there must be some way to solve it, if one
were to write an extension, since certainly the Visual
Studio debugger is able to launch non-console
applications but obtain their PID's.)


Sorry I can't be of more help,

Regards,

Bill
 
N

nobuyoshi nakada

Hi,

At Tue, 24 May 2005 14:40:17 +0900,
Martin aus Chemnitz wrote in [ruby-talk:143491]:
Yukihiro Matsumoto writes in
If you want to kill child processes when the thread is down,

th = Thread.new{
Thread.current[:children] = []
begin
... add child processes to Thread.current[:children]
ensure
Process.kill("TERM", *Thread.current[:children])
end
}
but I think I can't get the PID of a system call either.

Fundamentally, since Kernel.system doesn't finish until the child
process exits, so PID can not be available.
I would be glad if such a feature could be implemented. (Or, of course,
if anyone can tell me suitable solution.)

In 1.9, Kernel.spawn (or Process.spawn) does it.
 
N

nobuyoshi nakada

Hi,

At Tue, 24 May 2005 14:40:17 +0900,
Martin aus Chemnitz wrote in [ruby-talk:143491]:
Yukihiro Matsumoto writes in
If you want to kill child processes when the thread is down,

th = Thread.new{
Thread.current[:children] = []
begin
... add child processes to Thread.current[:children]
ensure
Process.kill("TERM", *Thread.current[:children])
end
}
but I think I can't get the PID of a system call either.

Fundamentally, since Kernel.system doesn't finish until the child
process exits, so PID can not be available.
I would be glad if such a feature could be implemented. (Or, of course,
if anyone can tell me suitable solution.)

In 1.9, Kernel.spawn (or Process.spawn) does it.
 
G

Gavin Kistner

Under Windows, I want to start an external program and kill it on
exitting my Ruby script.

begin
$t = Thread.new { system("everlasting") }
...
ensure
$t.kill
end

does not stop the everlasting program, it stops $t, not its child
processes. Such a command is urgently needed!

Here's a way to kill any process using Win32OLE:

require 'win32ole'

class WIN32OLE
def to_a
a = []; self.each{ |p| a<<p }; a
end
end

# Find the process for the player
mgmt = WIN32OLE.connect('winmgmts:\\\\.')
process = mgmt.InstancesOf("win32_process").to_a.find{ |proc|
proc.name =~ /myapp.exe/ }
process.Terminate
 
A

Ara.T.Howard

Under Windows, I want to start an external program and kill it on
exitting my Ruby script.

begin
$t = Thread.new { system("everlasting") }
...
ensure
$t.kill
end

does not stop the everlasting program, it stops $t, not its child
processes. Such a command is urgently needed!

Here's a way to kill any process using Win32OLE:

require 'win32ole'

class WIN32OLE
def to_a
a = []; self.each{ |p| a<<p }; a
end
end

# Find the process for the player
mgmt = WIN32OLE.connect('winmgmts:\\\\.')
process = mgmt.InstancesOf("win32_process").to_a.find{ |proc|
proc.name =~ /myapp.exe/ }
process.Terminate

so no way by pid?

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| My religion is very simple. My religion is kindness.
| --Tenzin Gyatso
===============================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top