0
-# Copyright (c) 2007 Marc-André Cournoyer
0
+# Simplified version of Thin::Daemonizable by Marc-André Cournoyer
0
unless respond_to? :daemonize # Already part of Ruby 1.9, yeah!
0
@@ -29,27 +28,13 @@ module Process
0
module_function :running?
0
-# Module included in classes that can be turned into a daemon.
0
+# Moadule included in classes that can be turned into a daemon.
0
# * storing the PID in a file
0
# * redirecting output to the log file
0
-# * changing processs privileges
0
# * killing the process gracefully
0
- attr_accessor :pid_file, :log_file
0
- def self.included(base)
0
- base.extend ClassMethods
0
- def daemonizable_init(options)
0
- @pid_file = options[:pid_file]
0
- @log_file = options[:log_file]
0
- if options[:daemonize]
0
- change_privilege options[:user], options[:group] if options[:user] && options[:group]
0
+ attr_accessor :pid_file, :log_file, :timeout
0
File.exist?(pid_file) ? open(pid_file).read : nil
0
@@ -60,11 +45,11 @@ module Daemonizable
0
raise ArgumentError, 'You must specify a pid_file to deamonize' unless @pid_file
0
pwd = Dir.pwd # Current directory is changed during daemonization, so store it
0
- super # Calls Kernel#daemonize
0
trap('HUP', 'IGNORE') # Don't die upon logout
0
# Redirect output to the logfile
0
[STDOUT, STDERR].each { |f| f.reopen @log_file, 'a' } if @log_file
0
@@ -75,60 +60,39 @@ module Daemonizable
0
- # Change privileges of the process
0
- # to the specified user and group.
0
- def change_privilege(user, group=user)
0
- log ">> Changing process privilege to #{user}:#{group}"
0
- uid, gid = Process.euid, Process.egid
0
- target_uid = Etc.getpwnam(user).uid
0
- target_gid = Etc.getgrnam(group).gid
0
- if uid != target_uid || gid != target_gid
0
- # Change process ownership
0
- Process.initgroups(user, target_gid)
0
- Process::GID.change_privilege(target_gid)
0
- Process::UID.change_privilege(target_uid)
0
- rescue Errno::EPERM => e
0
- log "Couldn't change user and group to #{user}:#{group}: #{e}"
0
- # Kill the process which PID is stored in +pid_file+.
0
- def kill(pid_file, timeout=60)
0
- if pid = open(pid_file).read
0
- print "Sending INT signal to process #{pid} ... "
0
- Process.kill('INT', pid)
0
- Timeout.timeout(timeout) do
0
- sleep 0.1 while Process.running?(pid)
0
- print "timeout, Sending KILL signal ... "
0
- Process.kill('KILL', pid)
0
+ # Kill the process which PID is stored in +pid_file+.
0
+ def self.kill(pid_file, timeout=60)
0
+ if pid = open(pid_file).read
0
+ print "Sending INT signal to process #{pid} ... "
0
+ Process.kill('INT', pid)
0
+ Timeout.timeout(timeout) do
0
+ sleep 0.1 while Process.running?(pid)
0
- puts "Can't stop process, no PID found in #{@pid_file}"
0
+ print "timeout, Sending KILL signal ... "
0
+ Process.kill('KILL', pid)
0
- rescue Errno::ESRCH # No such process
0
- puts "process not found!"
0
- File.delete(pid_file) rescue nil
0
+ puts "Can't stop process, no PID found in #{@pid_file}"
0
+ rescue Errno::ESRCH # No such process
0
+ puts "process not found!"
0
+ File.delete(pid_file) rescue nil
0
- File.delete(@pid_file) if @pid_file && File.exists?(@pid_file)
0
- log ">> Writing PID to #{@pid_file}"
0
- FileUtils.mkdir_p File.dirname(@pid_file)
0
- open(@pid_file,"w") { |f| f.write(Process.pid) }
0
- File.chmod(0644, @pid_file)
0
+ File.delete(@pid_file) if @pid_file && File.exists?(@pid_file)
0
+ log ">> Writing PID to #{@pid_file}"
0
+ open(@pid_file,"w+") { |f| f.write(Process.pid) }
0
+ File.chmod(0644, @pid_file)
Comments
No one has commented yet.