Method: Daemonize.daemonize
- Defined in:
- lib/dctl/daemonize.rb
.daemonize(stdin = '/dev/null', stdout = '/dev/null', stderr = '/dev/null', oldmode = true) ⇒ Object
This method causes the current running process to become a daemon
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/dctl/daemonize.rb', line 107 def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null', oldmode=true) stdin = File. stdin stdout = File. stdout stderr = File. stderr srand # Split rand streams between spawning and daemonized process safefork and exit # Fork and exit from the parent # Detach from the controlling terminal unless sess_id = Process.setsid raise SystemCallError, 'cannot detach from controlled terminal' end # Prevent the possibility of acquiring a controlling terminal if oldmode trap 'SIGHUP', 'IGNORE' safefork and exit end Dir.chdir "/" # Release old working directory File.umask 0000 # Insure sensible umask # Make sure all file descriptors are closed ObjectSpace.each_object(IO) do |io| unless [STDIN, STDOUT, STDERR].include?(io) io.close rescue nil end end STDIN.reopen stdin # Free file descriptors and STDOUT.reopen stdout, "a" # point them somewhere sensible STDERR.reopen stderr, "a" # STDOUT/STDERR should go to a logfile sess_id # Return value is mostly irrelevant end |