Update task.py

pull/109/head
midoks 6 years ago
parent 5d70800bc0
commit fbb80776e5
  1. 79
      task.py

@ -45,9 +45,10 @@ def async(f):
thr.start() thr.start()
return wrapper return wrapper
@async @async
def restartMw(): def restartMw():
sleep(1) time.sleep(1)
cmd = public.getRunDir() + '/scripts/init.d/mw restart' cmd = public.getRunDir() + '/scripts/init.d/mw restart'
public.execShell(cmd) public.execShell(cmd)
@ -360,11 +361,12 @@ def check502Task():
try: try:
while True: while True:
if os.path.exists(public.getRunDir() + '/data/502Task.pl'): if os.path.exists(public.getRunDir() + '/data/502Task.pl'):
check502(); check502()
time.sleep(30); time.sleep(30)
except: except:
time.sleep(30); time.sleep(30)
check502Task(); check502Task()
def check502(): def check502():
try: try:
@ -372,8 +374,10 @@ def check502():
for version in phpversions: for version in phpversions:
sdir = public.getServerDir() sdir = public.getServerDir()
php_path = sdir + '/php/' + version + '/sbin/php-fpm' php_path = sdir + '/php/' + version + '/sbin/php-fpm'
if not os.path.exists(php_path): continue; if not os.path.exists(php_path):
if checkPHPVersion(version): continue; continue
if checkPHPVersion(version):
continue
if startPHPVersion(version): if startPHPVersion(version):
print '检测到PHP-' + version + '处理异常,已自动修复!' print '检测到PHP-' + version + '处理异常,已自动修复!'
public.writeLog('PHP守护程序', '检测到PHP-' + version + '处理异常,已自动修复!') public.writeLog('PHP守护程序', '检测到PHP-' + version + '处理异常,已自动修复!')
@ -388,59 +392,70 @@ def startPHPVersion(version):
fpm = sdir + '/php/init.d/php' + version fpm = sdir + '/php/init.d/php' + version
php_path = sdir + '/php/' + version + '/sbin/php-fpm' php_path = sdir + '/php/' + version + '/sbin/php-fpm'
if not os.path.exists(php_path): if not os.path.exists(php_path):
if os.path.exists(fpm): os.remove(fpm) if os.path.exists(fpm):
return False; os.remove(fpm)
return False
# 尝试重载服务 # 尝试重载服务
os.system(fpm + ' reload'); os.system(fpm + ' reload')
if checkPHPVersion(version): return True; if checkPHPVersion(version):
return True
# 尝试重启服务 # 尝试重启服务
cgi = '/tmp/php-cgi-' + version + '.sock' cgi = '/tmp/php-cgi-' + version + '.sock'
pid = sdir+'/php/'+version+'/var/run/php-fpm.pid'; pid = sdir + '/php/' + version + '/var/run/php-fpm.pid'
data = public.execShell("ps -ef | grep php/"+version+" | grep -v grep|grep -v python |awk '{print $2}'") data = public.execShell("ps -ef | grep php/" + version +
" | grep -v grep|grep -v python |awk '{print $2}'")
if data[0] != '': if data[0] != '':
os.system("ps -ef | grep php/"+version+" | grep -v grep|grep -v python |awk '{print $2}' | xargs kill ") os.system("ps -ef | grep php/" + version +
time.sleep(0.5); " | grep -v grep|grep -v python |awk '{print $2}' | xargs kill ")
if not os.path.exists(cgi): os.system('rm -f ' + cgi); time.sleep(0.5)
if not os.path.exists(pid): os.system('rm -f ' + pid); if not os.path.exists(cgi):
os.system(fpm + ' start'); os.system('rm -f ' + cgi)
if checkPHPVersion(version): return True; if not os.path.exists(pid):
os.system('rm -f ' + pid)
os.system(fpm + ' start')
if checkPHPVersion(version):
return True
# 检查是否正确启动 # 检查是否正确启动
if os.path.exists(cgi): return True; if os.path.exists(cgi):
return True
except Exception as e: except Exception as e:
print str(e) print str(e)
return True; return True
# 检查指定PHP版本 # 检查指定PHP版本
def checkPHPVersion(version): def checkPHPVersion(version):
try: try:
url = 'http://127.0.0.1/phpfpm_status_'+version; url = 'http://127.0.0.1/phpfpm_status_' + version
result = public.httpGet(url); result = public.httpGet(url)
# print version,result # print version,result
# 检查nginx # 检查nginx
if result.find('Bad Gateway') != -1: return False; if result.find('Bad Gateway') != -1:
if result.find('HTTP Error 404: Not Found') != -1: return False; return False
if result.find('HTTP Error 404: Not Found') != -1:
return False
# 检查Web服务是否启动 # 检查Web服务是否启动
if result.find('Connection refused') != -1: if result.find('Connection refused') != -1:
global isTask global isTask
if os.path.exists(isTask): if os.path.exists(isTask):
isStatus = public.readFile(isTask); isStatus = public.readFile(isTask)
if isStatus == 'True': return True; if isStatus == 'True':
filename = '/etc/init.d/openresty'; return True
if os.path.exists(filename): os.system(filename + ' start'); filename = '/etc/init.d/openresty'
return True; if os.path.exists(filename):
os.system(filename + ' start')
return True
except: except:
return True; return True
# --------------------------------------PHP监控 end--------------------------------------------- # # --------------------------------------PHP监控 end--------------------------------------------- #
if __name__ == "__main__": if __name__ == "__main__":
t = threading.Thread(target=systemTask) t = threading.Thread(target=systemTask)
t.setDaemon(True) t.setDaemon(True)
t.start() t.start()

Loading…
Cancel
Save