1 #! /usr/bin/env python 2 #coding=utf-8 3 4 import sys 5 import ConfigParser 6 import urllib 7 import urllib2 8 from util import http_server 9 from logger import logger 10 import json 11 import traceback 12 import time 13 14 reload(sys) 15 sys.setdefaultencoding('utf-8') 16 17 import os 18 import signal 19 20 Max_process = 2 21 log_index = 0 22 23 # start python program 24 def start(): 25 global Max_process 26 global log_index 27 path = os.popen('pwd').readlines()[0] 28 print path,'===' 29 os.system('nohup python serverdate.py &') 30 time.sleep(10) 31 32 #stop child 33 def stopChild(): 34 processInfo = os.popen("ps -ef|grep serverdate.py|grep -v grep|awk '{print $2}'").readlines() 35 print processInfo,'------' 36 for pid in processInfo: 37 os.kill(int(pid),signal.SIGKILL) 38 39 #stop parent 40 def stopParent(): 41 parentList = os.popen("ps -ef|grep serverdate.py|grep -v grep|awk '{print $2}'").readlines() 42 print parentList,'***' 43 for pid in parentList: 44 os.kill(int(pid),signal.SIGKILL) 45 46 def stopAll(): 47 stopChild() 48 stopParent() 49 50 try: 51 fun = sys.argv[1] 52 except Exception: 53 fun = '' 54 if(fun == 'start'): 55 start() 56 if(fun == 'stop'): 57 stopAll() 58 if(fun == 'restart'): 59 stopAll() 60 time.sleep(10) 61 start()
使用方法:python serverdate.py start|stop|restart