Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Clean up stale PID files when daemonizing.
  • Loading branch information
chuyeow committed Mar 3, 2008
1 parent a3967a9 commit 49599e5
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/thin/daemonizing.rb
Expand Up @@ -34,11 +34,20 @@ def pid

# Turns the current script into a daemon process that detaches from the console.
def daemonize
raise PlatformNotSupported, 'Daemonizing not supported on Windows' if Thin.win?
raise PlatformNotSupported, 'Daemonizing is not supported on Windows' if Thin.win?
raise ArgumentError, 'You must specify a pid_file to daemonize' unless @pid_file
raise PidFileExist, "#{@pid_file} already exist, seems like it's already running. " +
"Stop the process or delete #{@pid_file}." if File.exist?(@pid_file)


# If PID file is stale, remove it.
if File.exist?(@pid_file)
if pid && Process.running?(pid)
raise PidFileExist, "#{@pid_file} already exists, seems like it's already running (process ID: #{pid}). " +
"Stop the process or delete #{@pid_file}."
else
log ">> Deleting stale PID file #{@pid_file}"
remove_pid_file
end
end

pwd = Dir.pwd # Current directory is changed during daemonization, so store it

Daemonize.daemonize(File.expand_path(@log_file), name)
Expand All @@ -53,7 +62,7 @@ def daemonize
remove_pid_file
end
end

# Change privileges of the process
# to the specified user and group.
def change_privilege(user, group=user)
Expand All @@ -73,12 +82,12 @@ def change_privilege(user, group=user)
log "Couldn't change user and group to #{user}:#{group}: #{e}"
end

# Registerer a proc to be called to restart the server.
# Register a proc to be called to restart the server.
def on_restart(&block)
@on_restart = block
end

# Restart the server
# Restart the server.
def restart
raise ArgumentError, "Can't restart, no 'on_restart' proc specified" unless @on_restart
log '>> Restarting ...'
Expand Down Expand Up @@ -108,7 +117,7 @@ def kill(pid_file, timeout=60)
File.delete(pid_file) if File.exist?(pid_file)
end

# Restart the server by sending HUP signal
# Restart the server by sending HUP signal.
def restart(pid_file)
send_signal('HUP', pid_file)
end
Expand Down

0 comments on commit 49599e5

Please sign in to comment.