#!/usr/bin/env python
from time import sleep
from sys import argv, exit, stderr
import signal
import os
RUNDIR='/var/run/racktop'
PIDFILE='%s/dummy.pid' % RUNDIR
def cleanup():
os.unlink(pidfile)
exit(0)
def handler(signum, frame):
print "Caught Signal. Terminating..."
cleanup()
def idle(time=30):
print "Sleeping for %d seconds" % time
sleep(time)
if len(argv[:]) > 1:
key, val = argv[1].split('=')
if key == '--pidfile':
pidfile=val
else:
pidfile=PIDFILE
try:
mypid = os.getpid()
signal.signal(signal.SIGTERM, handler)
try:
if not os.path.exists(os.path.dirname(pidfile)):
os.mkdir(os.path.dirname(pidfile))
except OSError as e:
stderr.write("Failed to create run directory: %s" % e.strerror)
try:
with open(pidfile, 'wb') as pf:
pf.write("%d\n" % mypid)
except OSError as e:
stderr.write("Failed to write PID to file: %s" % e.strerror)
while True:
idle()
except KeyboardInterrupt as e:
print "Kill Signal Received. Exiting..."
cleanup()