qwatch.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "kill_pid " + 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. pass
  36. except BaseException, be:
  37. pass
  38. return
  39. def sig_handler(sig,frame):
  40. global shutdown
  41. global queue_procs
  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_procs != None:
  48. for proc in queue_procs:
  49. proc.terminate()
  50. class Daemon(object):
  51. def __init__(self,stdin='/dev/null',stdout='/dev/null',stderr='/dev/null'):
  52. self.stdin = stdin
  53. self.stdout = stdout
  54. self.stderr = stderr
  55. def register_handler(self):
  56. signal.signal(signal.SIGINT, sig_handler)
  57. signal.signal(signal.SIGTERM, sig_handler)
  58. signal.signal(signal.SIGCHLD, sig_handler)
  59. def daemonize(self):
  60. try:
  61. pid = os.fork()
  62. if pid != 0:
  63. sys.exit(0)
  64. except OSError, e:
  65. sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
  66. sys.exit(1)
  67. os.setsid()
  68. os.umask(0)
  69. try:
  70. pid = os.fork()
  71. if pid != 0:
  72. sys.exit(0)
  73. except OSError, e:
  74. sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
  75. sys.exit(1)
  76. os.close(0)
  77. os.close(1)
  78. os.close(2)
  79. file(self.stdin, 'r')
  80. file(self.stdout, 'a+')
  81. file(self.stderr, 'a+')
  82. def mac_sys():
  83. plat_name = sys.platform
  84. return (plat_name == 'darwin')
  85. def main():
  86. global queue_procs
  87. kill_pid("qwatch.py")
  88. kill_pid("crontab.php")
  89. queue_procs = list()
  90. # d = Daemon()
  91. # d.daemonize()
  92. # d.register_handler()
  93. print "cur_pid =" , os.getpid()
  94. if mac_sys() :
  95. cmds = ["php", "./crontab.php", "queue", "index"]
  96. else:
  97. cmds = ["/usr/local/php/bin/php", "./crontab.php", "queue", "index"]
  98. while(shutdown == False):
  99. for i in range(0,10):
  100. subproc = subprocess.Popen(cmds,subprocess.PIPE)
  101. queue_procs.append(subproc)
  102. print "create queue porcess pid=",subproc.pid
  103. for proc in queue_procs:
  104. proc.wait()
  105. if __name__ == '__main__':
  106. main()