Re: Spawning daemon processes



On Fri, 1 Sep 2006, Thiago Arrais wrote:

Is there any idiomatic way of doing that in Ruby that is platform independent?

I'd just use Daniel Berger's win32utils:

http://rubyforge.org/projects/win32utils/

You need win32-process (which needs windows-pr) out of there, and then you can easily use create_process on Windows. Here's one way that I use it to provide a method for spawning new processes that works where fork exists and on Windows, as well.

module IWATestSupport
def self.create_process(args)
# Takes a hash with two keys, :cmd and :dir.
# :cmd is the command to execute, and :dir is the directory to
# execute the command from.
args[:cmd] = [args[:cmd]] unless args[:cmd].is_a?(Array)
@fork_ok = true unless @fork_ok == false
pid = nil
begin
raise NotImplementedError unless @fork_ok
unless pid = fork
Dir.chdir args[:dir]
exec(*args[:cmd])
end
rescue NotImplementedError
@fork_ok = false
begin
require 'rubygems'
rescue Exception
end

begin
require 'win32/process'
rescue LoadError
raise "Please install win32-process.\n'gem install win32-process' or http://rubyforge.org/projects/win32utils";
end
cwd = Dir.pwd
Dir.chdir args[:dir]
pid = Process.create(:app_name => args[:cmd].join(' '))
Dir.chdir cwd
end
pid
end
end


IWATestSupport.create_process({:dir => 'TC_CGI_Adaptor',
:cmd => [ruby,'-I../../../src','webrick.rb']})


There are a lot of very nice things in Dan's win32utils.


Kirk Haines

.



Relevant Pages

  • Re: Start an external application - How?
    ... For this purpose I mainly use `start "" progname.exe` (note the double quotes: ... unless pid = fork ... rescue NotImplementedError ... Dir.chdir cwd ...
    (comp.lang.ruby)
  • Re: How do you use flock and clean up lock files?
    ... If your need is really closer to a fork & exec than just a fork, ... unless pid = fork ... rescue NotImplementedError ... Dir.chdir cwd ...
    (comp.lang.ruby)
  • Re: determining if a process is alive on windows
    ... def alive pid ... rescue Errno::ESRCH ... It works for me on windows too, ...
    (comp.lang.ruby)
  • Re: OE Generates c0000005 (access violation) How to correct?
    ... You really should visit the Windows Update page to update your system to SP2 ... >> George Hester ... The PID changes so using ...
    (microsoft.public.windows.inetexplorer.ie6_outlookexpress)
  • Re: CPU pegs near 100%, mysterious log entry
    ... For some mysterious reason Windows has been busy resynching the mirror drive. ... This is Windows RAID (I decided I could not trust the motherboard's built in Raid after a few days of trying the machine way back when. ... Now, I don't know whether it is resynching because of the hard shutdown, or because of something that happened before I had to do a hard reboot. ... I had tried the tasklist /svc for the svchost.exe PID but it wast not very helpful. ...
    (microsoft.public.windows.server.sbs)