qwatch.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python
  2. import sys, os, signal, subprocess, shlex,time,atexit
  3. shutdown = False
  4. queue_procs = 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. global queue_procs
  43. frame = None
  44. if sig == signal.SIGINT:
  45. shutdown = True
  46. elif sig == signal.SIGTERM:
  47. shutdown = True
  48. if shutdown == True and queue_procs != None:
  49. for proc in queue_procs:
  50. proc.terminate()
  51. class Daemon(object):
  52. def __init__(self,stdin='/dev/null',stdout='/dev/null',stderr='/dev/null'):
  53. self.stdin = stdin
  54. self.stdout = stdout
  55. self.stderr = stderr
  56. def register_handler(self):
  57. signal.signal(signal.SIGINT, sig_handler)
  58. signal.signal(signal.SIGTERM, sig_handler)
  59. signal.signal(signal.SIGCHLD, sig_handler)
  60. def daemonize(self):
  61. try:
  62. pid = os.fork()
  63. if pid != 0:
  64. sys.exit(0)
  65. except OSError, e:
  66. sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
  67. sys.exit(1)
  68. os.setsid()
  69. os.umask(0)
  70. try:
  71. pid = os.fork()
  72. if pid != 0:
  73. sys.exit(0)
  74. except OSError, e:
  75. sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
  76. sys.exit(1)
  77. os.close(0)
  78. os.close(1)
  79. os.close(2)
  80. file(self.stdin, 'r')
  81. file(self.stdout, 'a+')
  82. file(self.stderr, 'a+')
  83. def main():
  84. global queue_procs
  85. kill_pid("qwatch.py")
  86. kill_pid("crontab.php")
  87. queue_procs = list()
  88. d = Daemon()
  89. d.daemonize()
  90. d.register_handler()
  91. while(shutdown == False):
  92. cmds = ["php", "./crontab.php", "queue", "index"]
  93. for i in range(0,10):
  94. subproc = subprocess.Popen(cmds,subprocess.PIPE)
  95. queue_procs.append(subproc)
  96. print "create queue porcess pid=",subproc.pid
  97. for proc in queue_procs:
  98. proc.wait()
  99. if __name__ == '__main__':
  100. main()