Module included in classes that can be turned into a daemon. Handle stuff like:
storing the PID in a file
redirecting output to the log file
changing processs privileges
killing the process gracefully
Change privileges of the process to the specified user and group.
# File lib/thin/daemonizing.rb, line 62 62: def change_privilege(user, group=user) 63: log ">> Changing process privilege to #{user}:#{group}" 64: 65: uid, gid = Process.euid, Process.egid 66: target_uid = Etc.getpwnam(user).uid 67: target_gid = Etc.getgrnam(group).gid 68: 69: if uid != target_uid || gid != target_gid 70: # Change process ownership 71: Process.initgroups(user, target_gid) 72: Process::GID.change_privilege(target_gid) 73: Process::UID.change_privilege(target_uid) 74: end 75: rescue Errno::EPERM => e 76: log "Couldn't change user and group to #{user}:#{group}: #{e}" 77: end
Turns the current script into a daemon process that detaches from the console.
# File lib/thin/daemonizing.rb, line 36 36: def daemonize 37: raise PlatformNotSupported, 'Daemonizing is not supported on Windows' if Thin.win? 38: raise ArgumentError, 'You must specify a pid_file to daemonize' unless @pid_file 39: 40: remove_stale_pid_file 41: 42: pwd = Dir.pwd # Current directory is changed during daemonization, so store it 43: 44: # HACK we need to create the directory before daemonization to prevent a bug under 1.9 45: # ignoring all signals when the directory is created after daemonization. 46: FileUtils.mkdir_p File.dirname(@pid_file) 47: 48: Daemonize.daemonize(File.expand_path(@log_file), name) 49: 50: Dir.chdir(pwd) 51: 52: write_pid_file 53: 54: at_exit do 55: log ">> Exiting!" 56: remove_pid_file 57: end 58: end
Register a proc to be called to restart the server.
# File lib/thin/daemonizing.rb, line 80 80: def on_restart(&block) 81: @on_restart = block 82: end
# File lib/thin/daemonizing.rb, line 153 153: def remove_pid_file 154: File.delete(@pid_file) if @pid_file && File.exists?(@pid_file) 155: end
If PID file is stale, remove it.
# File lib/thin/daemonizing.rb, line 164 164: def remove_stale_pid_file 165: if File.exist?(@pid_file) 166: if pid && Process.running?(pid) 167: raise PidFileExist, "#{@pid_file} already exists, seems like it's already running (process ID: #{pid}). " + 168: "Stop the process or delete #{@pid_file}." 169: else 170: log ">> Deleting stale PID file #{@pid_file}" 171: remove_pid_file 172: end 173: end 174: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.