Sha256: cd0de621aea52206387fb9c1cbf89748c49c08a949f4ad45e1b7369f995ac0fc

Contents?: true

Size: 1.12 KB

Versions: 9

Compression:

Stored size: 1.12 KB

Contents

module LifxDash
  class Daemonizer

    LOG_FILE = "/tmp/lifx_dash.log"

    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

    # Free the STDIN/STDOUT/STDERR file descriptors and point them somewhere
    # sensible - inspired by daemons gem
    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
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
lifx_dash-0.3.1 lib/lifx_dash/daemonizer.rb
lifx_dash-0.3.0 lib/lifx_dash/daemonizer.rb
lifx_dash-0.2.3 lib/lifx_dash/daemonizer.rb
lifx_dash-0.2.2 lib/lifx_dash/daemonizer.rb
lifx_dash-0.2.1 lib/lifx_dash/daemonizer.rb
lifx_dash-0.2.0 lib/lifx_dash/daemonizer.rb
lifx_dash-0.1.3 lib/lifx_dash/daemonizer.rb
lifx_dash-0.1.1 lib/lifx_dash/daemonizer.rb
lifx_dash-0.1.0 lib/lifx_dash/daemonizer.rb