Create a daemon with double-fork in Ruby Create a daemon with double-fork in Ruby linux linux

Create a daemon with double-fork 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:

  1. Fork and have the parent exit. This makes the shell or boot script think the command is done. Also, the child process is guaranteed not to be a process group leader (a prerequisite for setsid next)
  2. Call setsid to create a new session. This does three things:
    1. The process becomes a session leader of a new session
    2. The process becomes the process group leader of a new process group
    3. The process has no controlling terminal
  3. Optionally fork again and have the parent exit. This guarantes that the daemon is not a session leader nor can it acquire a controlling terminal (under SVR4)
  4. Change the current working directory to / to avoid interfering with mounting and unmounting
  5. Set file mode creation mask to 000 to allow creation of files with any required permission later.
  6. Close unneeded file descriptors inherited from the parent (there is no controlling terminal anyway): stdout, 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 != 0raise 'First fork failed' if (pid = fork) == -1exit unless pid.nil?Process.setsidraise 'Second fork failed' if (pid = fork) == -1exit unless pid.nil?puts "Daemon pid: #{Process.pid}" # Or save it somewhere, etc.Dir.chdir '/'File.umask 0000STDIN.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