Skip to content

Instantly share code, notes, and snippets.

@dux
Created July 4, 2019 10:46
Show Gist options
  • Save dux/b2e429c61337c41c7908bde02cc55905 to your computer and use it in GitHub Desktop.
Save dux/b2e429c61337c41c7908bde02cc55905 to your computer and use it in GitHub Desktop.
check if process pid is running and exit
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