Spawning daemon processes

T

Thiago Arrais

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 =3D 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 =3D 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 independe=
nt?

Cheers,

Thiago Arrais
--=20
Mergulhando no Caos - http://thiagoarrais.blogspot.com
Pensamentos, id=E9ias e devaneios sobre desenvolvimento de software e
tecnologia em geral
 
K

khaines

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
 
G

Garance A Drosehn

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

Thiago Arrais

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top