Module: Daemonize

Included in:
PatternPark::FCSHD
Defined in:
lib/airake/daemonize.rb

Overview

Daemonize code provided by Travis Whitton

grub.ath.cx/daemonize/

Instance Method Summary collapse

Instance Method Details

#daemonize(oldmode = 0, closefd = false) ⇒ Object

This method causes the current running process to become a daemon

If closefd is true, all existing file descriptors are closed



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/airake/daemonize.rb', line 28

def daemonize(oldmode=0, closefd=false)
  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 'Cannot detach from controlled terminal'
  end

  # Prevent the possibility of acquiring a controlling terminal
  if oldmode.zero?
    trap 'SIGHUP', 'IGNORE'
    exit if pid = safefork
  end

  Dir.chdir "/"   # Release old working directory
  File.umask 0000 # Insure sensible umask

  if closefd
    # 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
  end

  STDIN.reopen "/dev/null"       # Free file descriptors and
  STDOUT.reopen "/dev/null", "a" # point them somewhere sensible
  STDERR.reopen STDOUT           # STDOUT/STDERR should go to a logfile
  return oldmode ? sess_id : 0   # Return value is mostly irrelevant
end

#safeforkObject

Try to fork if at all possible retrying every 5 sec if the maximum process limit for the system has been reached



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/airake/daemonize.rb', line 9

def safefork
  tryagain = true

  while tryagain
    tryagain = false
    begin
      if pid = fork
        return pid
      end
    rescue Errno::EWOULDBLOCK
      sleep 5
      tryagain = true
    end
  end
end