# Spawning daemon processes

**URL:** <https://rubytalk.org/t/spawning-daemon-processes/30524>\
**Category:** ruby-talk\
**Created:** [31 August 2006 17:07 UTC](https://rubytalk.org/t/spawning-daemon-processes/30524 "2006-08-31T17:07:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thiago\_Arrais1](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@Thiago\_Arrais1](https://rubytalk.org/u/Thiago_Arrais1)\
**Post date:** [31 August 2006 17:07 UTC](https://rubytalk.org/t/spawning-daemon-processes/30524/1 "2006-08-31T17:07:10Z")

</div>

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  
&nbsp;&nbsp;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  
&nbsp;&nbsp;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](http://thiagoarrais.blogspot.com)  
> Pensamentos, idéias e devaneios sobre desenvolvimento de software e  
> tecnologia em geral

---

<div class="post-metadata">

**Author:** ![Kirk\_Haines](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Kirk\_Haines](https://rubytalk.org/u/Kirk_Haines)\
**Post date:** [31 August 2006 17:41 UTC](https://rubytalk.org/t/spawning-daemon-processes/30524/2 "2006-08-31T17:41:33Z")

</div>

I'd just use Daniel Berger's win32utils:

[http://rubyforge.org/projects/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  
&nbsp;&nbsp;&nbsp;def self.create\_process(args)  
&nbsp;&nbsp;&nbsp;# Takes a hash with two keys, :cmd and :dir.  
&nbsp;&nbsp;&nbsp;# :cmd is the command to execute, and :dir is the directory to  
&nbsp;&nbsp;&nbsp;# execute the command from.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args[:cmd] = [args[:cmd]] unless args[:cmd].is\_a?(Array)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@fork\_ok = true unless @fork\_ok == false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pid = nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise NotImplementedError unless @fork\_ok  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless pid = fork  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dir.chdir args[:dir]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exec(\*args[:cmd])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue NotImplementedError  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@fork\_ok = false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'rubygems'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue Exception  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

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

IWATestSupport.create\_process({:dir =\> 'TC\_CGI\_Adaptor',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: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?

---

<div class="post-metadata">

**Author:** ![Garance\_A\_Drosehn](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@Garance\_A\_Drosehn](https://rubytalk.org/u/Garance_A_Drosehn)\
**Post date:** [1 September 2006 01:33 UTC](https://rubytalk.org/t/spawning-daemon-processes/30524/3 "2006-09-01T01:33:52Z")

</div>

> 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  
> &nbsp;&nbsp;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/](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

---

<div class="post-metadata">

**Author:** ![M\_Edward\_Ed\_Borasky1](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@M\_Edward\_Ed\_Borasky1](https://rubytalk.org/u/M_Edward_Ed_Borasky1)\
**Post date:** [1 September 2006 04:00 UTC](https://rubytalk.org/t/spawning-daemon-processes/30524/4 "2006-09-01T04:00:38Z")

</div>

Thiago Arrais wrote:

> [snip]  
> Thiago Arrais

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

\<ducking\>

---

<div class="post-metadata">

**Author:** ![Thiago\_Arrais1](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@Thiago\_Arrais1](https://rubytalk.org/u/Thiago_Arrais1)\
**Post date:** [1 September 2006 20:23 UTC](https://rubytalk.org/t/spawning-daemon-processes/30524/5 "2006-09-01T20:23:25Z")

</div>

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
