Daemonizing Ruby Processes(September 15, 2014)
Your application might need a long running process in order to:
- Listen on a socket (e.g. a web server)
- Subscribe to a queue (e.g. a resque worker)
- Poll a DB job table (e.g. a DelayedJob)
Much of the time you will use an appropriate 3rd party server such as Unicorn, Resque, or RackRabbit, but sometimes you just want to write some Ruby code and make it daemonizable. To achieve this you might reach for gems such as Dante or Daemon-kit, and these are good options, but with a little work you can avoid the additional gem dependency and get a deeper appreciation for the underlying Unix process mechanisms along the way.
As part of my work on the RackRabbit server I had to learn a little more about daemonizing and forking unix processes, and so I wrote up a little how-to for those of you who might want to daemonize your own Ruby code without adding any new dependencies.Continued...