Class: LifxDash::Daemonizer

Inherits:
Object
  • Object
show all
Defined in:
lib/lifx_dash/daemonizer.rb

Constant Summary collapse

LOG_FILE =
"/tmp/lifx_dash.log"

Class Method Summary collapse

Class Method Details

.redirect_io(log_file) ⇒ Object

Free the STDIN/STDOUT/STDERR file descriptors and point them somewhere sensible - inspired by daemons gem


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lifx_dash/daemonizer.rb', line 16

def self.redirect_io(log_file)
  STDIN.reopen '/dev/null'

  begin
    # ensure log file exists with good permissions
    FileUtils.mkdir_p(File.dirname(log_file), :mode => 0755)
    FileUtils.touch log_file
    File.chmod(0644, log_file)

    # reopen STOUT stream to file
    STDOUT.reopen log_file, 'a'
    STDOUT.sync = true
  rescue ::StandardError
    STDOUT.reopen '/dev/null'
  end

  # reopen STERR stream to STDOUT (file stream)
  STDERR.reopen STDOUT
  STDERR.sync = true
rescue => e
  raise "#{self} - error: could not redirect IO - #{e.message}"
end

.start(log_file = LOG_FILE) ⇒ Object


6
7
8
9
10
11
12
# File 'lib/lifx_dash/daemonizer.rb', line 6

def self.start(log_file = LOG_FILE)
  # fork process (skip IO redirect to /dev/null)
  Process.daemon(false, true)
  # show pid and log file info on stdout right away
  puts "[#{Process.pid}] Starting lifx_dash ... (daemon logging to #{log_file})"
  redirect_io(log_file)
end