# Communicate with a daemon

**URL:** <https://www.ruby-forum.com/t/communicate-with-a-daemon/167641>\
**Category:** Ruby\
**Created:** [May 10, 2009, 11:22pm UTC](https://www.ruby-forum.com/t/communicate-with-a-daemon/167641 "2009-05-10T23:22:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [May 10, 2009, 11:22pm UTC](https://www.ruby-forum.com/t/communicate-with-a-daemon/167641/1 "2009-05-10T23:22:13Z")

</div>

Hello everyone,

After daemonize a Ruby process (thanks Ruby version 1.9), it is possible  
to communicate with on the same machine by using socket on localhost. Do  
you know some other methods instead of socket?

Thanks.

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [May 10, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/communicate-with-a-daemon/167641/2 "2009-05-10T23:40:42Z")

</div>

From: “Paul A.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> After daemonize a Ruby process (thanks Ruby version 1.9), it is possible  
> to communicate with on the same machine by using socket on localhost. Do  
> you know some other methods instead of socket?

Perhaps [http://localmemcache.rubyforge.org/](http://localmemcache.rubyforge.org/)

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [May 11, 2009, 12:32am UTC](https://www.ruby-forum.com/t/communicate-with-a-daemon/167641/3 "2009-05-11T00:32:12Z")

</div>

On May 10, 2009, at 4:22 PM, Paul A. wrote:

> After daemonize a Ruby process (thanks Ruby version 1.9), it is  
> possible  
> to communicate with on the same machine by using socket on  
> localhost. Do  
> you know some other methods instead of socket?

Sure. In I daemon I’ve been working on, I just use SQLite as a queue  
for messages. I use the Amalgalite extension, which is terrific and  
works well on Ruby 1.9.

When you’re going to write to the queue, make sure to grab an  
“exclusive” lock (via the “transaction” SQL command). When reading,  
grab an “immediate” lock first. Set a busy handler through Amalgalite  
that retries for a reasonable time (I use 60 seconds) to grab locks.  
Add some error handling code for if the lock wait does eventually  
timeout, but that shouldn’t happen under normal operations.

About the only challenges I ran into were deleting data from the  
queue. Deleting counts as a write, so grab an “exclusive” lock. That  
was easy of course. The downside is that the SQLite database needs to  
have the “vacuum” command run on it every so often to reclaim wasted  
disk space. You can’t be holding a lock when you do that though.  
Thus I locked on an external source about once a day, and vacuumed the  
database. If it’s just a simple queue you need (with no long term  
storage), you could also just nuke the file and rebuild it periodically.

Anyway, I hope that gives you some fresh ideas.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [May 11, 2009, 8:26am UTC](https://www.ruby-forum.com/t/communicate-with-a-daemon/167641/4 "2009-05-11T08:26:09Z")

</div>

On 10.05.2009 23:22, Paul A. wrote:

> After daemonize a Ruby process (thanks Ruby version 1.9), it is possible  
> to communicate with on the same machine by using socket on localhost. Do  
> you know some other methods instead of socket?

You have got the whole range of IPC at your fingertips

- named FIFO,
- unnamed FIFO if the client is a child process of your daemon,
- Unix domain socket (only on the local machine),
- signals,
- shared memory (with proper locking, e.g. via semaphores),  
…

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [May 11, 2009, 11:16am UTC](https://www.ruby-forum.com/t/communicate-with-a-daemon/167641/5 "2009-05-11T11:16:46Z")

</div>

Robert K. wrote:

> On 10.05.2009 23:22, Paul A. wrote:
> 
> > After daemonize a Ruby process (thanks Ruby version 1.9), it is possible  
> > to communicate with on the same machine by using socket on localhost. Do  
> > you know some other methods instead of socket?
> 
> You have got the whole range of IPC at your fingertips
> 
> - named FIFO,
> - unnamed FIFO if the client is a child process of your daemon,
> - Unix domain socket (only on the local machine),
> - signals,
> - shared memory (with proper locking, e.g. via semaphores),  
> …

And don’t forget the simple DRb, if both ends are Ruby:

[http://web.archive.org/web/20070217144414rn\_1/wiki.rubygarden.org/Ruby/page/show/DrbTutorial](http://web.archive.org/web/20070217144414rn_1/wiki.rubygarden.org/Ruby/page/show/DrbTutorial)

Also XMLRPC (included in the Ruby standard library, at least it was for  
1.8, don’t know if it’s still in 1.9), and also plenty of ways to embed  
a HTTP server into your application (e.g. Webrick, Sinatra)
