Spawning daemon processes

I am trying to start and stop a daemon process from inside my Ruby
program. At first I tried using fork to do the job, as in

server_pid = fork do
  system 'runserver'
end

Then I could use the returned PID to send the process a signal by
using Process.kill. But, as far as i know, fork unfortunately doesn't
work on Windows. I read somewhere I should be using Thread.new if I
want the code to be platform independent, like this:

thread = Thread.new do
  system 'runserver'
end

The question is: how do I kill the subprocess when using threads? I
tried thread.kill and thread.stop, but they won't do the trick. When
using threads, what I get are a lot of orphan processes after the
initial Ruby program finishes.

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

Cheers,

Thiago Arrais

···

--
Mergulhando no Caos - http://thiagoarrais.blogspot.com
Pensamentos, idéias e devaneios sobre desenvolvimento de software e
tecnologia em geral

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

···

On Fri, 1 Sep 2006, Thiago Arrais wrote:

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

I am trying to start and stop a daemon process from inside my Ruby
program. At first I tried using fork to do the job, as in

server_pid = fork do
  system 'runserver'
end

Then I could use the returned PID to send the process a signal by
using Process.kill.

For unix, you might want to check out the rubygem called 'daemons'.
If you are not setup with rubygems, you could also download it from:

http://rubyforge.org/projects/daemons/

If what you are writing is meant to be a standard unix daemon, which
implies a number of things about how it should behave, then this
daemons package will handle many of the details that you might
not be aware of.

I only use the daemons package on unix, and I suspect that it does
not work on Windows.

But, as far as i know, fork unfortunately doesn't work on Windows.

My guess is that you need to write your script to do one set of things
under Unix, and something else when it is run under WIndows.
But maybe someone else will have a useful solution for you.

···

On 8/31/06, Thiago Arrais <thiago.arrais@gmail.com> wrote:

--
Garance Alistair Drosehn = drosihn@gmail.com
Senior Systems Programmer
Rensselaer Polytechnic Institute; Troy, NY; USA

Thiago Arrais wrote:

[snip]
Thiago Arrais

Be careful ... we don't want to have Ruby known as "daemon spawn", do we?

<ducking>

Kirk,

Thanks for pointer.

After some talking to Daniel, I could actually use his win32-process
library. It had a little bug that prevented it from being used under
Win2k, but he promptly fixed it.

Cheers,

Thiago Arrais