Created
July 4, 2019 10:46
-
-
Save dux/b2e429c61337c41c7908bde02cc55905 to your computer and use it in GitHub Desktop.
check if process pid is running and exit
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
class Asmodeus | |
attr_accessor :pid_file | |
def info text | |
puts 'Asmodeus: %s' % text | |
end | |
def pid_check no_check=false | |
return unless @pid_file | |
unless no_check | |
running = Process.getpgid(File.read(@pid_file).to_i) rescue false | |
if running | |
info 'running, exit' | |
exit | |
end | |
end | |
File.write(@pid_file, Process.pid) | |
end | |
def check | |
if block_given? && !yield | |
info 'func check failed' | |
pid_check true | |
else | |
pid_check | |
end | |
true | |
end | |
end | |
# | |
test = Asmodeus.new | |
test.pid_file = 'looper.pid' | |
test.check { true } | |
loop do | |
puts rand | |
sleep 0.5 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment