Created
April 21, 2010 07:28
-
-
Save jamiecobbett/373539 to your computer and use it in GitHub Desktop.
Pukka Ruby Daemon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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