Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def daemonize(name = File.basename($0), options = {})
pid_path = options[:pid_path] || File.expand_path("tmp/pids/#{name}.pid")
# If the pid file exists, check that the process is actually still running.
begin
if File.exists?(pid_path) && Process.kill(0, File.read(pid_path).to_i)
$stderr.puts "Already running."
exit 1
end
rescue Errno::ESRCH
File.delete(pid_path)
end
at_exit do
File.delete(pid_path)
logger.error("#{$!.class}: #{$!.message}\n#{$!.backtrace.join("\n")}") if $!
end
Process.daemon(true)
File.open(pid_path, "w") { |io| io.write(Process.pid) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.