# How to daemonize a Ruby program

**URL:** <https://www.ruby-forum.com/t/how-to-daemonize-a-ruby-program/170202>\
**Category:** Ruby\
**Created:** [June 13, 2009, 7:38pm UTC](https://www.ruby-forum.com/t/how-to-daemonize-a-ruby-program/170202 "2009-06-13T19:38:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 13, 2009, 7:38pm UTC](https://www.ruby-forum.com/t/how-to-daemonize-a-ruby-program/170202/1 "2009-06-13T19:38:00Z")

</div>

Hi, I’ve coded a Ruby server listening in TCP and UDP. For TCP I use  
GServer  
and for UDP an own code.

I need to daemonize it to get it working in background so I can manage  
the  
service using a Debian init script, but I have no idea of how to do it.

Basically I run the following:

tcp\_server = GServer.new(bind\_port, bind\_ip)  
tcp\_server.start  
tcp\_server.join

So, how could I get the programm running in background?  
This is, I must be able tu run the program in a terminal:  
./my\_server.rb  
and leave the same terminal free to use it while the daemon is running.

Thanks a lot for any suggestion.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 13, 2009, 7:56pm UTC](https://www.ruby-forum.com/t/how-to-daemonize-a-ruby-program/170202/2 "2009-06-13T19:56:08Z")

</div>

El SÃ¡bado, 13 de Junio de 2009, IÃ±aki Baz C. escribiÃ³:

> I need to daemonize it to get it working in background so I can manage the  
> service using a Debian init script, but I have no idea of how to do it.

I had no luck on my first Google search, but I’ve tryed now eith  
keywords  
“daemonize ruby” and got two possbile solutions :

1. Using “daemons” gem
2. Using “fokk” and Process.detach

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 13, 2009, 8:11pm UTC](https://www.ruby-forum.com/t/how-to-daemonize-a-ruby-program/170202/3 "2009-06-13T20:11:58Z")

</div>

El SÃ¡bado, 13 de Junio de 2009, IÃ±aki Baz C. escribiÃ³:

> El SÃ¡bado, 13 de Junio de 2009, IÃ±aki Baz C. escribiÃ³:
> 
> > I need to daemonize it to get it working in background so I can manage  
> > the service using a Debian init script, but I have no idea of how to do  
> > it.
> 
> I had no luck on my first Google search, but I’ve tryed now eith keywords  
> “daemonize ruby” and got two possbile solutions :
> 
> 1. Using “daemons” gem
> 2. Using “fokk” and Process.detach

I wonder why the following code doesn’t work at all:

—daemon1.rb----------------------  
#!/usr/bin/env ruby

require ‘daemons’

Daemons.daemonize

## loop do sleep 2 end

I run it (ruby daemon1.rb) and after it I check if there is any ruby  
process  
running:  
ps -ef | grep ruby

but it’s not running Â¿?Â¿?Â¿

Do I miss something? The above comes in the documentaion of Daemons:  
[http://daemons.rubyforge.org/](http://daemons.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 13, 2009, 8:43pm UTC](https://www.ruby-forum.com/t/how-to-daemonize-a-ruby-program/170202/4 "2009-06-13T20:43:35Z")

</div>

El SÃ¡bado, 13 de Junio de 2009, IÃ±aki Baz C. escribiÃ³:

> > 1. Using “fokk” and Process.detach  
> > loop do  
> > sleep 2  
> > end
> 
> * * *
> 
> I run it (ruby daemon1.rb) and after it I check if there is any ruby  
> process running:  
> ps -ef | grep ruby
> 
> but it’s not running Â¿?Â¿?Â¿

Ups, forget it please, it works 🙂

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 13, 2009, 9:19pm UTC](https://www.ruby-forum.com/t/how-to-daemonize-a-ruby-program/170202/5 "2009-06-13T21:19:54Z")

</div>

IÃ±aki Baz C. wrote:

> El SÃ¡bado, 13 de Junio de 2009, IÃ±aki Baz C. escribiÃ³:
> 
> > I need to daemonize it to get it working in background so I can manage the  
> > service using a Debian init script, but I have no idea of how to do it.
> 
> I had no luck on my first Google search, but I’ve tryed now eith  
> keywords  
> “daemonize ruby” and got two possbile solutions :
> 
> 1. Using “daemons” gem
> 2. Using “fokk” and Process.detach

Though I haven’t personally used it, you might want to take a look at  
the RobustThread \<URL:  
[SuperJared.com is for sale | HugeDomains](http://superjared.com/entry/ruby-daemons-using-robustthread/)\> library  
for your demonic[sic] needs. 🙂

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 15, 2009, 4:20pm UTC](https://www.ruby-forum.com/t/how-to-daemonize-a-ruby-program/170202/6 "2009-06-15T16:20:52Z")

</div>

On 2009-06-13, IÃ±aki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I need to daemonize it to get it working in background so I can manage the  
> service using a Debian init script, but I have no idea of how to do it.

The package ‘daemon’ is intended for this,  
[http://packages.debian.org/lenny/daemon](http://packages.debian.org/lenny/daemon)
