# Spawning daemon processes

**URL:** <https://www.ruby-forum.com/t/spawning-daemon-processes/67114>\
**Category:** Ruby\
**Created:** [August 31, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/spawning-daemon-processes/67114 "2006-08-31T19:08:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thiagoalessio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@thiagoalessio](https://www.ruby-forum.com/u/thiagoalessio)\
**Post date:** [August 31, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/spawning-daemon-processes/67114/1 "2006-08-31T19:08:41Z")

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

---

<div class="post-metadata">

**Author:** ![thiagoalessio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@thiagoalessio](https://www.ruby-forum.com/u/thiagoalessio)\
**Post date:** [September 1, 2006, 3:37am UTC](https://www.ruby-forum.com/t/spawning-daemon-processes/67114/2 "2006-09-01T03:37:35Z")

</div>

On 8/31/06, Thiago A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

---

<div class="post-metadata">

**Author:** ![thiagoalessio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@thiagoalessio](https://www.ruby-forum.com/u/thiagoalessio)\
**Post date:** [August 31, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/spawning-daemon-processes/67114/3 "2006-08-31T19:42:45Z")

</div>

On Fri, 1 Sep 2006, Thiago A. wrote:

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

I’d just use Daniel B.'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  
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](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 H.

---

<div class="post-metadata">

**Author:** ![thiagoalessio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@thiagoalessio](https://www.ruby-forum.com/u/thiagoalessio)\
**Post date:** [September 1, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/spawning-daemon-processes/67114/4 "2006-09-01T22:27:32Z")

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

---

<div class="post-metadata">

**Author:** ![thiagoalessio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@thiagoalessio](https://www.ruby-forum.com/u/thiagoalessio)\
**Post date:** [September 1, 2006, 6:03am UTC](https://www.ruby-forum.com/t/spawning-daemon-processes/67114/5 "2006-09-01T06:03:56Z")

</div>

Thiago A. wrote:

> [snip]  
> Thiago A.

Be careful … we don’t want to have Ruby known as “daemon spawn”, do  
we?
