Update task.py

pull/109/head
midoks 6 years ago
parent 5d70800bc0
commit fbb80776e5
  1. 115
      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)
@ -355,92 +356,106 @@ def systemTask():
# -------------------------------------- PHP监控 start --------------------------------------------- # # -------------------------------------- PHP监控 start --------------------------------------------- #
#502错误检查线程 # 502错误检查线程
def check502Task(): 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:
phpversions = ['53','54','55','56','70','71','72','73','74'] phpversions = ['53', '54', '55', '56', '70', '71', '72', '73', '74']
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 + '处理异常,已自动修复!')
except Exception as e: except Exception as e:
print str(e) print str(e)
#处理指定PHP版本 # 处理指定PHP版本
def startPHPVersion(version): def startPHPVersion(version):
sdir = public.getServerDir() sdir = public.getServerDir()
try: try:
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'); # 尝试重载服务
if checkPHPVersion(version): return True; os.system(fpm + ' reload')
if checkPHPVersion(version):
#尝试重启服务 return True
cgi = '/tmp/php-cgi-'+version + '.sock'
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}'") cgi = '/tmp/php-cgi-' + version + '.sock'
if data[0] != '' : pid = sdir + '/php/' + version + '/var/run/php-fpm.pid'
os.system("ps -ef | grep php/"+version+" | grep -v grep|grep -v python |awk '{print $2}' | xargs kill ") data = public.execShell("ps -ef | grep php/" + version +
time.sleep(0.5); " | grep -v grep|grep -v python |awk '{print $2}'")
if not os.path.exists(cgi): os.system('rm -f ' + cgi); if data[0] != '':
if not os.path.exists(pid): os.system('rm -f ' + pid); os.system("ps -ef | grep php/" + version +
os.system(fpm + ' start'); " | grep -v grep|grep -v python |awk '{print $2}' | xargs kill ")
if checkPHPVersion(version): return True; time.sleep(0.5)
if not os.path.exists(cgi):
#检查是否正确启动 os.system('rm -f ' + cgi)
if os.path.exists(cgi): 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
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