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/.