Daemon Processes : Process « Development « Ruby






Daemon Processes

def daemonize
  fork do
    Process.setsid
    exit if fork
    Dir.chdir('/')
    STDIN.reopen('/dev/null')
    STDOUT.reopen('/dev/null', 'a')

    STDERR.reopen('/dev/null', 'a')
    trap("TERM") { exit }
    yield
  end
end

daemonize do
  # You can do whatever you like in here and it will run in the background
  
end

puts "The daemon process has been launched!"

 








Related examples in the same category

1.use Process.wait to wait for all child processes to finish before continuing.
2.Use Process.kill
3.Process.pid and Process.ppid