Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
45 lines (35 sloc) 1.82 KB


This is a simple library for daemonizing scripts. It was extracted from Kenneth Kalmer's excellent daemon-kit project ( The daemon-kit framework is amazing for bootstrapping your daemons, but if you need something more lightweight (e.g., a single file daemon), then you're out of luck (for now). There are a number of daemon gem libraries available, but I was most impressed by the daemon-kit setup. This library lets you use the well thought out daemonization process from daemon-kit for simpler daemon setups.


You set configuration options as follows:

SimpleDaemon.setup do |config|
  config.daemon_name = "some_name"
  config.pid_file = "/var/run/"
  config.user = "some_user" = "some_group"

To actually daemonize your script, just run the code you want to run daemonized in a block as follows:

SimpleDaemon.daemonized do
  loop {
    # do something useful here

The pid file is automatically created and the script is backgrounded when run. When the process exits the pid file is cleaned up automatically. If the process dies or is forcibly killed with a KILL signal then the pid file will stay in place, but the daemonized blocks check to see if the process in the pid file is running or not. If it isn't, it will spin up a new process and replace the existing pid file. Keep in mind that there's no facility (yet) to handle the case where the process is still running, but no pid file exists.

Another thing to keep in mind is that the daemonized process is chroot'd to / and has its umask set to 0000. So, if you're writing out to files in the relative directory they'll show up in /, so you'll likely want to use full paths.


Copyright © 2010 Joel Watson. Portions copyright Kenneth Kalmer. See LICENSE for details.