qwatch.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python
  2. import sys, os, signal, subprocess, shlex,time,atexit
  3. shutdown = False
  4. queue_proc = None
  5. def file_pid(name):
  6. proc1 = subprocess.Popen(shlex.split('ps aux'), stdout=subprocess.PIPE)
  7. proc2 = subprocess.Popen(shlex.split('grep ' + name), stdin=proc1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  8. proc3 = subprocess.Popen(shlex.split('awk \'{print $2}\' '), stdin=proc2.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  9. proc1.stdout.close()
  10. proc2.stdout.close()
  11. out, err = proc3.communicate()
  12. lines = out.splitlines()
  13. pids = []
  14. for line in lines :
  15. item = line.strip()
  16. pid = int(item)
  17. if pid > 0:
  18. pids.append(pid)
  19. if len(pids) > 0 :
  20. return pids
  21. else:
  22. return []
  23. def kill_pid(name):
  24. print "start restart " + name
  25. cur_pid = os.getpid()
  26. pids = file_pid(name)
  27. for pid in pids:
  28. try:
  29. if cur_pid != pid:
  30. os.kill(pid, signal.SIGKILL)
  31. print 'kill pid=', pid
  32. else:
  33. continue
  34. except OSError, e:
  35. print "OSError no=", e.errno, " err=", e.strerror
  36. pass
  37. except BaseException, be:
  38. pass
  39. return
  40. def sig_handler(sig,frame):
  41. global shutdown
  42. frame = None
  43. if sig == signal.SIGINT:
  44. shutdown = True
  45. elif sig == signal.SIGTERM:
  46. shutdown = True
  47. if shutdown == True and queue_proc != None:
  48. queue_proc.terminate()
  49. class Daemon(object):
  50. def __init__(self,stdin='/dev/null',stdout='/dev/null',stderr='/dev/null'):
  51. self.stdin = stdin
  52. self.stdout = stdout
  53. self.stderr = stderr
  54. def register_handler(self):
  55. signal.signal(signal.SIGINT, sig_handler)
  56. signal.signal(signal.SIGTERM, sig_handler)
  57. signal.signal(signal.SIGCHLD, sig_handler)
  58. def daemonize(self):
  59. try:
  60. pid = os.fork()
  61. if pid != 0:
  62. sys.exit(0)
  63. except OSError, e:
  64. sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
  65. sys.exit(1)
  66. os.setsid()
  67. os.umask(0)
  68. try:
  69. pid = os.fork()
  70. if pid != 0:
  71. sys.exit(0)
  72. except OSError, e:
  73. sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
  74. sys.exit(1)
  75. os.close(0)
  76. os.close(1)
  77. os.close(2)
  78. file(self.stdin, 'r')
  79. file(self.stdout, 'a+')
  80. file(self.stderr, 'a+')
  81. def main():
  82. kill_pid("qwatch.py")
  83. d = Daemon()
  84. d.daemonize()
  85. d.register_handler()
  86. kill_pid("crontab.php")
  87. while(shutdown == False):
  88. cmds = ["php", "./crontab.php", "queue", "index"]
  89. subproc = subprocess.Popen(cmds,subprocess.PIPE)
  90. queue_proc = subproc
  91. print "create queue porcess pid=",subproc.pid
  92. subproc.wait()
  93. if __name__ == '__main__':
  94. main()