#!/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