Created
February 4, 2011 21:35
-
-
Save darrend/811821 to your computer and use it in GitHub Desktop.
This dirty dirty preamble allows you to drop a ruby script into a cronfile...it will kill the previous incarnation...so lame it is awesome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# kill the process if it is running | |
PIDFILE = "/var/run/stream_load.pid" | |
def read_pidfile | |
begin | |
File.open(PIDFILE,"r") { |file| return file.gets.to_i } | |
rescue | |
puts "pidfile not found or invalid" | |
end if File.exists?(PIDFILE) | |
puts "no pidfile readable" | |
nil | |
end | |
def verify_pid pid | |
puts pid | |
%x"ps p #{pid}".each_line do |line| | |
puts line | |
if(line =~ /#{Regexp.escape(__FILE__)}/) | |
puts "pid is valid" | |
return true | |
end | |
end | |
puts "pid is INvalid or process isn't running" | |
return false | |
end | |
pid = read_pidfile | |
unless pid.nil? | |
if verify_pid(pid) | |
puts "killing pid" | |
Process.kill "KILL", pid | |
else | |
puts "pid no longer valid" | |
end | |
else | |
puts "pid not found" | |
end | |
File.open(PIDFILE,"w") do|file| | |
puts "writing pid" | |
file.print Process.pid | |
end | |
# now start doing important things.... | |
while true | |
echo "awesome" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment