restart.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python
  2. import sys, os, signal, subprocess, shlex,time
  3. def file_pid(name):
  4. proc1 = subprocess.Popen(shlex.split('ps aux'), stdout=subprocess.PIPE)
  5. proc2 = subprocess.Popen(shlex.split('grep ' + name), stdin=proc1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  6. proc3 = subprocess.Popen(shlex.split('awk \'{print $2}\' '), stdin=proc2.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  7. proc1.stdout.close()
  8. proc2.stdout.close()
  9. out, err = proc3.communicate()
  10. lines = out.splitlines()
  11. pids = []
  12. for line in lines :
  13. item = line.strip()
  14. pid = int(item)
  15. if pid > 0:
  16. pids.append(pid)
  17. if len(pids) > 0 :
  18. return pids
  19. else:
  20. return []
  21. def has_proc(name):
  22. pids = file_pid(name)
  23. return len(pids) > 0
  24. def restart(item):
  25. name = item['file_name']
  26. cmd = item['cmd']
  27. if not name:
  28. print 'file name is empty'
  29. return
  30. cur_pid = os.getpid()
  31. pids = file_pid(name)
  32. print "start restart " + name
  33. for pid in pids:
  34. try:
  35. if cur_pid != pid:
  36. os.kill(pid, signal.SIGKILL)
  37. print 'kill pid=', pid
  38. else:
  39. continue
  40. except OSError, e:
  41. print "OSError no=", e.errno, " err=", e.strerror
  42. pass
  43. except BaseException, be:
  44. pass
  45. if cmd:
  46. print cmd
  47. time.sleep(1)
  48. os.system(cmd)
  49. time.sleep(1)
  50. return
  51. def mac_sys():
  52. plat_name = sys.platform
  53. return (plat_name == 'darwin')
  54. def main():
  55. print "you can input: fcgi,queue,ugc,center,all to restart server or no input it will restart fcgi,queue"
  56. if len(sys.argv) == 2:
  57. option = sys.argv[1]
  58. else:
  59. option = ''
  60. cmds = []
  61. if mac_sys() :
  62. if option == 'fcgi':
  63. cmds.append({'file_name': 'fcgi_run.php','cmd': 'spawn-fcgi -a 127.0.0.1 -p 9100 -F 1 -f "php fcgi_run.php"'})
  64. elif option == 'queue':
  65. cmds.append({'file_name': 'crontab.php','cmd': ''})
  66. elif option == 'ugc' :
  67. cmds.append({'file_name': 'mac_ugcman', 'cmd': 'sudo ./mac_ugcman'})
  68. cmds.append({'file_name': 'ugc_srv.php','cmd': 'php ugc_srv.php'})
  69. elif option == 'center':
  70. cmds.append({'file_name': 'centra_srv.php','cmd': 'php centra_srv.php'})
  71. elif option == 'all':
  72. cmds.append({'file_name': 'fcgi_run.php', 'cmd': 'spawn-fcgi -a 127.0.0.1 -p 9100 -F 1 -f "php fcgi_run.php"'})
  73. cmds.append({'file_name': 'crontab.php', 'cmd': ''})
  74. cmds.append({'file_name': 'mac_ugcman', 'cmd': './mac_ugcman'})
  75. cmds.append({'file_name': 'ugc_srv.php', 'cmd': 'php ugc_srv.php'})
  76. cmds.append({'file_name': 'centra_srv.php', 'cmd': 'php centra_srv.php'})
  77. else:
  78. cmds.append({'file_name': 'fcgi_run.php', 'cmd': 'spawn-fcgi -a 127.0.0.1 -p 9100 -F 1 -f "php fcgi_run.php"'})
  79. cmds.append({'file_name': 'crontab.php', 'cmd': ''})
  80. else :
  81. if option == 'fcgi':
  82. cmds.append({'file_name': 'fcgi_run.php', 'cmd': 'spawn-fcgi -a 127.0.0.1 -p 9100 -F 10 -f "php fcgi_run.php"'})
  83. elif option == 'queue':
  84. cmds.append({'file_name': 'crontab.php', 'cmd': ''})
  85. elif option == 'ugc':
  86. cmds.append({'file_name': 'ugcman', 'cmd': './ugcman'})
  87. cmds.append({'file_name': 'ugc_srv.php', 'cmd': 'php ugc_srv.php'})
  88. elif option == 'center':
  89. cmds.append({'file_name': 'centra_srv.php', 'cmd': 'php centra_srv.php'})
  90. elif option == 'all':
  91. cmds.append({'file_name': 'fcgi_run.php', 'cmd': 'spawn-fcgi -a 127.0.0.1 -p 9100 -F 10 -f "php fcgi_run.php"'})
  92. cmds.append({'file_name': 'crontab.php', 'cmd': ''})
  93. cmds.append({'file_name': 'ugcman', 'cmd': './ugcman'})
  94. cmds.append({'file_name': 'ugc_srv.php', 'cmd': 'php ugc_srv.php'})
  95. cmds.append({'file_name': 'centra_srv.php', 'cmd': 'php centra_srv.php'})
  96. else:
  97. cmds.append({'file_name': 'fcgi_run.php', 'cmd': 'spawn-fcgi -a 127.0.0.1 -p 9100 -F 10 -f "php fcgi_run.php"'})
  98. cmds.append({'file_name': 'crontab.php', 'cmd': ''})
  99. for item in cmds :
  100. restart(item)
  101. sys.exit(2)
  102. if __name__ == '__main__':
  103. main()