Revisions
-
mynameisrufus revised this gist
Mar 5, 2012 . 1 changed file with 19 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,16 +37,18 @@ class SimpleDaemon # Checks to see if the current process is the child process and if not # will update the pid file with the child pid. def self.start pid, pidfile, outfile, errfile unless pid.nil? raise "Fork failed" if pid == -1 write pid, pidfile if kill pid, pidfile exit else redirect outfile, errfile end end # Attempts to write the pid of the forked process to the pid file. def self.write pid, pidfile File.open pidfile, "w" do |f| f.write pid end @@ -74,18 +76,28 @@ def self.kill(pid, pidfile) $stderr.puts "While signaling the PID, unexpected #{e.class}: #{e}" false end # Send stdout and stderr to log files for the child process def self.redirect outfile, errfile $stdin.reopen '/dev/null' out = File.new outfile, "a" err = File.new errfile, "a" $stdout.reopen out $stderr.reopen err $stdout.sync = $stderr.sync = true end end # Process name of your daemon $0 = "simple ruby daemon" # Spawn a deamon SimpleDaemon.start fork, (ARGV[0] || '/tmp/deamon.pid'), (ARGV[1] || '/tmp/daemon.stdout.log'), (ARGV[2] || '/tmp/daemon.stderr.log') # Set up signals for our daemon, for now they just exit the process. Signal.trap("HUP") { $stdout.puts "SIGHUP and exit"; exit } Signal.trap("INT") { $stdout.puts "SIGINT and exit"; exit } Signal.trap("QUIT") { $stdout.puts "SIGQUIT and exit"; exit } # Remove this loop and replace with your own daemon logic. loop do -
mynameisrufus revised this gist
Nov 17, 2011 . 1 changed file with 8 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,13 +11,13 @@ # 1 second sleep. # # Most of the code is dedicated to managing a pid file. We want a pid # file so we can use a monitoring tool to make sure our daemon keeps # running. # # === Why? # # Writing a daemon sounds hard but as you can see is not that # complicated, so lets strip away the magic and just write some ruby. # # === Usage # @@ -29,7 +29,7 @@ # # $ ./simple_ruby_daemon.rb tmp/simple_ruby_daemon.pid # # check that it is running by running the following: # # $ ps aux | grep simple_ruby_daemon # @@ -58,16 +58,17 @@ def self.write(pid, pidfile) # Try and read the existing pid from the pid file and signal the # process. Returns true for a non blocking status. def self.kill(pid, pidfile) opid = open(pidfile).read.strip.to_i Process.kill "HUP", opid true rescue Errno::ENOENT $stdout.puts "#{pidfile} did not exist: Errno::ENOENT" true rescue Errno::ESRCH $stdout.puts "The process #{opid} did not exist: Errno::ESRCH" true rescue Errno::EPERM $stderr.puts "Lack of privileges to manage the process #{opid}: Errno::EPERM" false rescue ::Exception => e $stderr.puts "While signaling the PID, unexpected #{e.class}: #{e}" @@ -89,4 +90,4 @@ def self.kill(pid, pidfile) # Remove this loop and replace with your own daemon logic. loop do sleep 1 end -
mynameisrufus created this gist
Nov 17, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,92 @@ #!/usr/bin/env ruby # == Simple Daemon # # A simple ruby daemon that you copy and change as needed. # # === How does it work? # # All this program does is fork the current process (creates a copy of # itself) then exits, the fork (child process) then goes on to run your # daemon code. In this example we are just running a while loop with a # 1 second sleep. # # Most of the code is dedicated to managing a pid file. We want a pid # file so we can use a monitoring tool to make sure your daemon keeps # running. # # === Why? # # Writing a daemon sounds hard but as you can see is not that # complicated so lets strip away the magic and just write some ruby. # # === Usage # # You can run this daemon by running: # # $ ./simple_ruby_daemon.rb # # or with an optional pid file location as its first argument: # # $ ./simple_ruby_daemon.rb tmp/simple_ruby_daemon.pid # # You can check that it is running by running the following: # # $ ps aux | grep simple_ruby_daemon # # Author:: Rufus Post (mailto:rufuspost@gmail.com) class SimpleDaemon # Checks to see if the current process is the child process and if not # will update the pid file with the child pid. def self.start(pid, pidfile) unless pid.nil? raise "Fork failed" if pid == -1 write(pid, pidfile) if kill(pid, pidfile) exit end end # Attempts to write the pid of the forked process to the pid file. def self.write(pid, pidfile) File.open pidfile, "w" do |f| f.write pid end rescue ::Exception => e $stderr.puts "While writing the PID to file, unexpected #{e.class}: #{e}" Process.kill "HUP", pid end # Try and read the existing pid from the pid file and signal the # process. Returns true for a non blocking status. def self.kill(pid, pidfile) Process.kill "HUP", open(pidfile).read.strip.to_i true rescue Errno::ENOENT $stdout.puts "#{pidfile} did not exist: Errno::ENOENT" true rescue Errno::ESRCH $stdout.puts "Process for the existing PID was not running: Errno::ESRCH" true rescue Errno::EPERM $stderr.puts "Lack of privileges to manage #{pidfile}: Errno::EPERM" false rescue ::Exception => e $stderr.puts "While signaling the PID, unexpected #{e.class}: #{e}" false end end # Process name of your daemon $0 = "simple ruby daemon" # Spawn a deamon SimpleDaemon.start fork, (ARGV.first || '/tmp/simple_ruby_deamon.pid') # Set up signals for our daemon, for now they just exit the process. Signal.trap("HUP") { puts "SIGHUP and exit"; exit } Signal.trap("INT") { puts "SIGINT and exit"; exit } Signal.trap("QUIT") { puts "SIGQUIT and exit"; exit } # Remove this loop and replace with your own daemon logic. loop do sleep 1 end