This is a simple library for managing pid files. 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). In the mean time, there weren't any obvious, simple pid file management libraries that you could trust. This one works well and has facilities to ensure the process the pid file is pointing to is still running, so you can handle stale pid files easily.
Some samples of the most common uses:
require 'rubygems' require 'simple_pid' pid = .("/path/to/your.pid") if pid.exists? unless pid.running? pid.cleanup pid.write! end else pid.write! end
Most of the above is fairly self-explanatory. You can accomplish all of the above by calling:
If the path specified for the pid is unwritable for any reason, it falls back to using /tmp to store the
You can cleanup quickly with:
The above cleanup class method returns true if the process wasn't running and the cleanup was actually run. It returns false if the process was still running and no cleanup occurred. If you need to force cleanup regardless of whether the process is running or not, you can do so with:
You can check to see if the process is running and exit with an error message if it is like this:
The current contents of the pid file can always be accessed like this:
Copyright © 2010 Joel Watson. Portions copyright Kenneth Kalmer. See LICENSE for details.