svcdaemon.py 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
import os
import subprocess
import time
import signal
import sys

def get_pid(filename):
  pid = None
  if os.path.exists(filename):
    data = open(pid_file).read()
    try:
      pid = int(data)
    except ValueError:
      pass
  return pid

pid_file = None
def sig_handler(s, frame):
  print "Killing on signal %s:" % s,
  global pid_file
  if pid_file is not None:
    pid = get_pid(pid_file)
    if pid is not None:
      os.kill(pid, signal.SIGTERM)
      try:
        os.kill(pid, 0)
      except Exception:
        pass
      else:
        time.sleep(5)
        try:
          os.kill(pid, 0)
        except Exception:
          pass
        else:
          print 'with SIGKILL...',
          os.kill(pid, signal.SIGKILL)
    else:
      raise ValueError('Pid is none.')
  print 'done.'
  sys.exit(0)

signal.signal(signal.SIGINT, sig_handler)
signal.signal(signal.SIGQUIT, sig_handler)
signal.signal(signal.SIGTERM, sig_handler)


def svcdaemon(args):
  """Utility script to run daemons in supervisord"""
  real_binary = args[0]
  global pid_file
  pid_file = args[1]
  subprocess.check_call(real_binary)
  print 'Started %r' % real_binary
  while True:
    time.sleep(5)
    pid = get_pid(pid_file)
    if pid is None:
      raise ValueError('Pid is none')
    os.kill(pid, 0)

if __name__ == '__main__':
  args = []
  args.append(sys.argv[1])
  args.append(sys.argv[2])
  if len(sys.argv) > 3:
    time.sleep(int(sys.argv[3]))
  svcdaemon(args)
  exit(0)