Skip to content

Instantly share code, notes, and snippets.

@davidpelaez
Created March 11, 2014 06:22
Show Gist options
  • Save davidpelaez/9480530 to your computer and use it in GitHub Desktop.
Save davidpelaez/9480530 to your computer and use it in GitHub Desktop.
Must use systemd at least 208
#!/usr/bin/env ruby
lockfile = File.new("/tmp/monitor.lock","w+")
unless lockfile.flock( File::LOCK_NB | File::LOCK_EX )
puts "%%%%%%%%%%%%%%%%%%%%%%%%% Only an instance can run at a time!"
exit! 1
end
Signal.trap "KILL" do puts "Exiting (TERM)"; exit! 0 end
Signal.trap "INT" do puts "Exiting (INT)"; exit! 0 end
def enabled?
str = File.read File.expand_path('../enabled', __FILE__)
str.to_i == 1 ? true : false
end
def ping
pid = Process.pid
puts ". #{ENV['WATCHDOG_USEC']}"
system "/usr/bin/systemd-notify MAINPID=#{pid} WATCHDOG=1"
end
puts "################ Starting ping app"
loop do
if enabled?
ping
else
puts "[#{Process.pid}] Skipping ping"
end
sleep 1
end
[Unit]
Description=PingMonitor
[Service]
ExecStart=/root/ping.rb
WatchdogSec=5s
Restart=on-failure
NotifyAccess=all
TimeoutStopSec=1s
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment