Win32-open3 module: giving a name to a Process

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

···

On 6/12/06, Jatinder Singh <jatinder.saundh@gmail.com> wrote:

Thanks this open3.so works!

-Jatinder

On 6/12/06, Park Heesob <phasis68@hotmail.com> wrote:
>
> Hi,
>
> >From: "Jatinder Singh" <jatinder.saundh@gmail.com>
> >Reply-To: ruby-talk@ruby-lang.org
> >To: ruby-talk@ruby-lang.org (ruby-talk ML)
> >Subject: Re: Instructions for downloading and installing win32-open3
> module
> >Date: Mon, 12 Jun 2006 21:45:52 +0900
> >
> >Hi,
> >
> >I was able to get around with the installation by renaming the so file
to
> >open3.so and putting it in the mentioned folder, but now on executing
the
> >ruby program I get following error,
> >
> >check.rb:3: [BUG] Segmentation fault
> >ruby 1.8.4 (2005-12-24) [i386-mswin32]
> >
> >This application has requested the Runtime to terminate it in an
unusual
> >way.
> >Please contact the application's support team for more information.
> >Following are the contents of ruby program,
> >
> >require "win32/open3"
> >
> >input, out, err, @pid = Open4.popen4('dir')
> >
> >Help Appreciated!
> >
> >Thanks
> >
> >Jatinder
> >
> Same as win32-serivce.so, you can download "open3.so" at
> http://home.nownuri.net/~phasis/open3.so
> and copy to ruby\lib\ruby\site_ruby\1.8\i386-msvcrt\win32 directory
>
> Soon or later, the segfaulting binary files will be replaced with
correct
> version.
>
> Regards,
>
> Park Heesob
>

Hi,

From: "Jatinder Singh" <jatinder.saundh@gmail.com>
Reply-To: ruby-talk@ruby-lang.org
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: Re: win32-open3 module: giving a name to a Process
Date: Tue, 13 Jun 2006 13:39:38 +0900

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!

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 Heesob

Park Heesob 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 Heesob

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

···

--
Posted via http://www.ruby-forum.com/\.