Module: Daemonize
- Included in:
- Dctl::Daemon
- Defined in:
- lib/dctl/pidfile.rb,
lib/dctl/daemonize.rb
Defined Under Namespace
Classes: PidFile
Constant Summary collapse
- VERSION =
"0.1.1n"
Class Method Summary collapse
-
.daemonize(stdin = '/dev/null', stdout = '/dev/null', stderr = '/dev/null', oldmode = true) ⇒ Object
This method causes the current running process to become a daemon.
-
.safefork ⇒ Object
Try to fork if at all possible retrying every 5 sec if the maximum process limit for the system has been reached.
Class Method Details
.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 |
.safefork ⇒ Object
Try to fork if at all possible retrying every 5 sec if the maximum process limit for the system has been reached
95 96 97 98 99 100 101 102 |
# File 'lib/dctl/daemonize.rb', line 95 def safefork begin fork rescue Errno::EWOULDBLOCK sleep 5 retry end end |