Skip to content

Instantly share code, notes, and snippets.

@jamiecobbett
Created April 21, 2010 07:28
Show Gist options
  • Save jamiecobbett/373539 to your computer and use it in GitHub Desktop.
Save jamiecobbett/373539 to your computer and use it in GitHub Desktop.
Pukka Ruby Daemon
# http://stackoverflow.com/questions/1740308/create-a-daemon-with-double-fork-in-ruby
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment