# Re: win32-open3 module: giving a name to a Process

**URL:** <https://www.ruby-forum.com/t/re-win32-open3-module-giving-a-name-to-a-process/63206>\
**Category:** Ruby\
**Created:** [June 13, 2006, 6:40am UTC](https://www.ruby-forum.com/t/re-win32-open3-module-giving-a-name-to-a-process/63206 "2006-06-13T06:40:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jatinder\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e4f/32.png) [@Jatinder\_S](https://www.ruby-forum.com/u/Jatinder_S)\
**Post date:** [June 13, 2006, 6:40am UTC](https://www.ruby-forum.com/t/re-win32-open3-module-giving-a-name-to-a-process/63206/1 "2006-06-13T06:40:45Z")

</div>

Hi,

Now I need to create a process with a name(the process is an instance of  
webrick server).  
I can create a webrick instance using following command,(this is not a  
blocking call, it returns immediately…thats what I want)  
input, out, err, @pid = Open4.popen4(‘D:\ruby\bin\ruby  
C:\InstantRails\rails\_apps\app1\script\server -p 3002’)

Now at later point of time I would like to kill this process which I can  
ofcourse do using  
Process.kill(9, @pid)

It may very well happen that process was killed externally, hence in  
that  
case I am first finding out if process with @pid is still alive by  
calling  
Process.kill(0,@pid) (it gives an error it the process is not alive,  
which I  
am catching)

My assumption in above case is that the webrick instance I had created  
earlier using popen4 method is still running with @pid; it may very well  
happen that webrick instance was killed externally and @pid was taken up  
by  
some other process; this will result in killing of a innocent process.  
I was thinking if I could assign a name to a process and then later on  
while  
killing, verify the name, then that would solve the problem.  
But how do I assign a name to a process while creating a process using  
popen4 method and then how do I find the pid of a process by name.

Help is appreciated!

Regards,  
Jatinder

---

<div class="post-metadata">

**Author:** ![Jatinder\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e4f/32.png) [@Jatinder\_S](https://www.ruby-forum.com/u/Jatinder_S)\
**Post date:** [June 13, 2006, 8:40am UTC](https://www.ruby-forum.com/t/re-win32-open3-module-giving-a-name-to-a-process/63206/2 "2006-06-13T08:40:20Z")

</div>

Hi,

> I can create a webrick instance using following command,(this is not a  
> Process.kill(0,@pid) (it gives an error it the process is not alive, which  
> But how do I assign a name to a process while creating a process using  
> popen4 method and then how do I find the pid of a process by name.
> 
> Help is appreciated!

How about this:

def get\_cmd(pid)  
require ‘win32ole’  
procs = WIN32OLE.connect(“winmgmts:\\.”)  
procs.ExecQuery(  
"SELECT CommandLine FROM Win32\_Process WHERE ProcessId = #{pid}  
",“WQL”).each { |p|  
return p.CommandLine  
}  
end

cmd = ‘D:\ruby\bin\ruby C:\InstantRails\rails\_apps\app1\script\server -p  
3002’  
input, out, err, @pid = Open4.popen4(cmd)

if cmd == get\_cmd(@pid)  
puts “Valid Process”  
Process.kill(0,@pid)  
else  
puts “Invalid Process”  
end

Regards,  
Park H.

---

<div class="post-metadata">

**Author:** ![Jatinder\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e4f/32.png) [@Jatinder\_S](https://www.ruby-forum.com/u/Jatinder_S)\
**Post date:** [June 13, 2006, 10:16am UTC](https://www.ruby-forum.com/t/re-win32-open3-module-giving-a-name-to-a-process/63206/3 "2006-06-13T10:16:42Z")

</div>

Park H. wrote:

> Hi,
> 
> > I can create a webrick instance using following command,(this is not a  
> > Process.kill(0,@pid) (it gives an error it the process is not alive, which  
> > But how do I assign a name to a process while creating a process using  
> > popen4 method and then how do I find the pid of a process by name.
> > 
> > Help is appreciated!
> 
> How about this:
> 
> def get\_cmd(pid)  
> require ‘win32ole’  
> procs = WIN32OLE.connect(“winmgmts:\\.”)  
> procs.ExecQuery(  
> "SELECT CommandLine FROM Win32\_Process WHERE ProcessId = #{pid}  
> ",“WQL”).each { |p|  
> return p.CommandLine  
> }  
> end
> 
> cmd = ‘D:\ruby\bin\ruby C:\InstantRails\rails\_apps\app1\script\server -p  
> 3002’  
> input, out, err, @pid = Open4.popen4(cmd)
> 
> if cmd == get\_cmd(@pid)  
> puts “Valid Process”  
> Process.kill(0,@pid)  
> else  
> puts “Invalid Process”  
> end
> 
> Regards,  
> Park H.

I think he wants to kill it ONLY if his application started it…

require ‘win32ole’

def is\_server\_running(pid)

running = false  
my\_pid = Process.pid  
procs = WIN32OLE.connect(“winmgmts:\\.”)  
#if a record is returned, we know that OUR process is still running  
running = (procs.ExecQuery("SELECT CommandLine FROM Win32\_Process  
WHERE ParentProcessId = #{my\_pid} AND ProcessId = #{pid} ",“WQL”).count

> = 1)

return running  
end

#CHANGE THIS LINE TO THE PID OF YOUR PROCESS  
pid = 4764;

if is\_server\_running(pid)  
puts “Valid Process”  
Process.kill(9,pid)  
else  
puts “Invalid Process”  
end
