72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/jah/agent.rb', line 72
def daemonize
begin
File.open(pid_file, File::CREAT|File::EXCL|File::WRONLY) do |pid|
pid.puts $$
end
at_exit do
begin
File.unlink(pid_file)
rescue
Log.error "Unable to unlink pid file: #{$!.message}"
end
end
rescue
pid = File.read(pid_file).strip.to_i rescue "unknown"
running = true
begin
Process.kill(0, pid)
if stat = File.stat(pid_file)
if mtime = stat.mtime
if Time.now - mtime > 25 * 60 Log.info "Trying to KILL an old process..."
Process.kill("KILL", pid)
running = false
end
end
end
rescue Errno::ESRCH
running = false
rescue
end
if running
if pid == "unknown"
Log.warn "Could not create or read PID file. " +
"You may need to the path to the config directory. " +
"See: http://scoutapp.com/help#data_file"
else
Log.warn "Process #{pid} was already running"
end
exit
else
Log.info "Stale PID file found. Clearing it and reloading..."
File.unlink(pid_file) rescue nil
retry
end
end
self
end
|