What is the proper way to create a well-behaved Unix or Linux daemon in Ruby?
What is the definition of a well-behaved daemon anyway, and how would one write such a program in Ruby?
What is the proper way to create a well-behaved Unix or Linux daemon in Ruby?
What is the definition of a well-behaved daemon anyway, and how would one write such a program in Ruby?
According to Stevens's Advanced Programming in the UNIX Environment chapter 13, this is the procedure to make a well-behaved Unix daemon:
setsid
to create a new session. This does three things:
/
to avoid interfering with mounting and unmountingstdout
, stderr
, and stdin
.Nowadays there is a file to track the PID which is used heavily by Linux distribution boot scripts. Be sure to write out the PID of the grandchild, either the return value of the second fork (step 3) or the value of getpid()
after step 3.
Here is a Ruby implementation, mostly translated from the book, but with the double-fork and writing out the daemon PID.
# Example double-forking Unix daemon initializer.
raise 'Must run as root' if Process.euid != 0
raise 'First fork failed' if (pid = fork) == -1
exit unless pid.nil?
Process.setsid
raise 'Second fork failed' if (pid = fork) == -1
exit unless pid.nil?
puts "Daemon pid: #{Process.pid}" # Or save it somewhere, etc.
Dir.chdir '/'
File.umask 0000
STDIN.reopen '/dev/null'
STDOUT.reopen '/dev/null', 'a'
STDERR.reopen STDOUT
Following on from Jason's awesome response I have written a fuller implementation here:
https://gist.github.com/1372491/b76b60fb1842bf0507f47869ab19ad50a045b214
I have implemented logging in addition to the double fork and writing of the pid to file.
Another interesting implementation is in Unicorn:
https://github.com/defunkt/unicorn/blob/master/lib/unicorn/launcher.rb