Sha256: 152c2799b78166be1ab491f5f96c218f4c49cf9d1cd061beedf7a79fb2c6833b
Contents?: true
Size: 1.03 KB
Versions: 2
Compression:
Stored size: 1.03 KB
Contents
#!/usr/bin/env ruby # Files name = File.basename($PROGRAM_NAME) $PROGRAM_NAME=name pid_file = "/tmp/#{name}.pid" log_file = "/tmp/#{name}.log" # Run only run one daemon at a time. if File.exists?(pid_file) File.open(log_file, 'a') { |f| f.puts "PID file exists already!" } exit end # Daemonize if RUBY_VERSION < "1.9" exit if fork Process.setsid exit if fork STDIN.reopen "/dev/null" STDOUT.reopen "/dev/null", "a" STDERR.reopen "/dev/null", "a" else Process.daemon end # Register signal handlers. at_exit { File.delete(pid_file) } [:TERM, :INT, :QUIT, :HUP].each do |signal| trap(signal) do File.open(log_file, 'a') do |f| f.puts "Daemon received #{signal} signal." f.puts "Daemon #{(signal == :HUP && 'refreshed') || 'stopped'}." end exit unless signal == :HUP end end # Forked childs write a PID file on startup. File.open(pid_file, 'w') { |f| f.puts(Process.pid) } # Overwrite log file. File.open(log_file, 'w') { |f| f.puts 'Daemon started...' } # Finally run the lazy daemon loop { sleep 1 }
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
foreground-0.1.0 | bin/foreground_sample_daemon |
foreground-0.0.4 | bin/foreground_sample_daemon |