simple_daemon
This is a simple library for daemonizing scripts. It was extracted from Kenneth Kalmer’s excellent daemon-kit project (github.com/kennethkalmer/daemon-kit). 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.
Usage
You set configuration options as follows:
SimpleDaemon.setup do |config
config.daemon_name = "some_name"
config.pid_file = "/var/run/some_name.pid"
end
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
}
end
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 is 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.
Copyright
Copyright © 2010 Joel Watson. Portions copyright Kenneth Kalmer. See LICENSE for details.