Skip to content

Instantly share code, notes, and snippets.

@istepanov
Created December 12, 2016 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save istepanov/a1d706fb48138795c0ab0d7e50f39bbc to your computer and use it in GitHub Desktop.
Save istepanov/a1d706fb48138795c0ab0d7e50f39bbc to your computer and use it in GitHub Desktop.
Simple ping watchdog
#!/usr/bin/env python
import os
import datetime
address = '8.8.8.8'
ping_timeout = 5
attempts = 4
log_file = '/var/log/ping_test.log'
failed_attempts = 0
for i in range(0, attempts):
result = os.system('/bin/ping -c 1 -w %i %s > /dev/null 2>&1' % (ping_timeout, address))
if result != 0:
failed_attempts += 1
if failed_attempts == attempts:
with open(log_file, 'a') as logfile:
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
logfile.write("%s 'ping %s' failed attempts: %i. Rebooting...\n" % (now, address, failed_attempts))
os.system('/sbin/reboot')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment