Skip to content

Instantly share code, notes, and snippets.

@jwhiteman
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwhiteman/139ce4b5267fcc29b2ef to your computer and use it in GitHub Desktop.
Save jwhiteman/139ce4b5267fcc29b2ef to your computer and use it in GitHub Desktop.
ruby daemon
def daemonize_app
if RUBY_VERSION < "1.9"
exit if fork # jettison the terminal
Process.setsid # establish a new group & session leader (can only be done as a child process)
exit if fork # jettison the group leader. now it can't be interrupted from another group or session going down.
Dir.chdir "/" # change dir to root to make sure that the dir won't go away during the process run
# as a demon we won't be needing these...
STDIN.reopen "/dev/null"
STDOUT.reopen "/dev/null", 'a'
STDERR.reopen "/dev/null", 'a'
else
Process.daemon # :)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment