Method: Chef::Daemon.daemonize

Defined in:
lib/chef/daemon.rb

.daemonize(name) ⇒ Object

Daemonize the current process, managing pidfiles and process uid/gid

=== Parameters name:: The name to be used for the pid file



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chef/daemon.rb', line 35

def daemonize(name)
  @name = name
  @runlock = RunLock.new(pid_file)
  if runlock.test
    # We've acquired the daemon lock. Now daemonize.
    Chef::Log.info("Daemonizing..")
    begin
      exit if fork
      Process.setsid
      exit if fork
      Chef::Log.info("Forked, in #{Process.pid}. Privileges: #{Process.euid} #{Process.egid}")
      File.umask Chef::Config[:umask]
      $stdin.reopen("/dev/null")
      $stdout.reopen("/dev/null", "a")
      $stderr.reopen($stdout)
      runlock.save_pid
    rescue NotImplementedError => e
      Chef::Application.fatal!("There is no fork: #{e.message}")
    end
  else
    Chef::Application.fatal!("Chef is already running pid #{pid_from_file}")
  end
end