自动化服务停止脚本
1、服务停止脚本,可根据脚本路径,脚本名,服务等进行停止。针对于端口号状态是否在监听来判断
import os import sys import time from _utils.patrol2 import run_cmd import platform def get_Linux_status(stop_port): status_cmd = "netstat -anp |grep LISTEN | grep %s | wc -l"%stop_port port_cmd = "netstat -anp |grep LISTEN | grep %s |awk '{print $7}' |awk -F'/' '{print $1}'" %stop_port print "\n################################ Automation ################################" print "检查服务状态:{}".format(status_cmd) print "################################ Automation ################################" code,output = run_cmd(status_cmd) code,output1 = run_cmd(port_cmd) if int(output.strip()) == 0: return False,output1 #异常 elif int(output.strip()) >= 1: return True,output1 #运行中 def get_windows_port(stop_port): cmd='netstat -ano | findstr {}'.format(stop_port) doce,ret = run_cmd(cmd)if '%s' %(stop_port) in ret and "LISTENING" in ret: print "{}查询端口存在服务运行中...".format(stop_port) output = True #服务运行中 else: print "{}查询端口不存在...".format(stop_port) output = False #服务断开 #sys.exit(1) return output def Linux_port(stop_port): status_cmd = "netstat -anp |grep LISTEN | grep %s" %stop_port code,ret = run_cmd(status_cmd) if '%s' %(stop_port) in ret: print "{}查询端口运行中...".format(stop_port) output = True #服务运行中 else: print "{}查询端口不存在...".format(stop_port) output = False #服务断开 #sys.exit(1) return output def stop_status(stop_port,time_num): #循环环判断服务是否停止成功,当前设定3,理论上最多会出现30次 "服务正在停止...... flag = 1 while flag < time_num: if get_windows_port(stop_port): print "{}服务正在停止......".format(service_name) flag+=1 time.sleep(10) else: print "{}服务已停止".format(service_name) break #print "{} 服务停止失败".format(service_name) # sys.exit(1) def stop_status_linux(stop_port,time_num): flag = 1 while flag < time_num: if Linux_port(stop_port): print "{}端口正在停止......".format(service_name) flag+=1 time.sleep(10) else: print "{}服务已停止".format(service_name) break '''***************************** 停止服务函数-stop() 路径文件的结合 例如:/bin/start.sh *****************************''' def stop(user,Stop_Script_Path,service_name, stop_port,Stop_Script,time_num,common_stop_server): if sys.platform.startswith('win'): if get_windows_port(stop_port): if common_stop_server: print "服务运行中,开始执行停止服务" cmd = "net stop %s" %common_stop_server print "执行停止命令:%s" %cmd code,output = run_cmd(cmd) time.sleep(15) if code == 0: print "执行关闭命令成功" print "查询端口是否运行" if get_windows_port(stop_port): cmd = "netstat -ano | findstr %s"%int(stop_port) code, out = run_cmd(cmd) ret_repo = out.split()[4] #获取PID print "获取端口号%s,开始执行杀死端口"%ret_repo cmd = "taskkill /pid %s -t -f" %ret_repo code, out = run_cmd(cmd) if code == 0: print "%s执行杀死端口成功"%cmd print out stop_status(stop_port,time_num) #循环环判断服务是否停止成功 else: print "%s执行杀死端口失败退出"%cmd sys.exit(1) else: print "%s端口状态不处于 LISTENING 中,已停止"%stop_port else: print "执行关闭命令失败退出" sys.exit(1) else: print "服务运行中,开始执行停止服务" # print Stop_Script_Path,'-------------' # sys.exit(1) os.chdir("%s" %Stop_Script_Path) stop_cmd = '{}'.format(Stop_Script) print "执行停止服务:{}".format(stop_cmd) code,output = run_cmd(stop_cmd) if code == 0: print "执行关闭命令成功" stop_status(stop_port,time_num) #循环环判断服务是否停止成功 else: print "执行关闭命令失败退出" sys.exit(1) else: print "{} 服务未运行,请确认".format(service_name) sys.exit(1) elif sys.platform.startswith('linux'): if get_Linux_status(stop_port)[0]: print "服务运行中,开始执行停止服务" Stop_Script_Path_1 = os.path.join(Stop_Script_Path, Stop_Script) stop_cmd ="su - {} -c 'sh {}'".format(user, Stop_Script_Path_1) print "执行停止服务:{}".format(stop_cmd) code,output = run_cmd(stop_cmd) if code == 0: print "执行关闭命令成功" if get_Linux_status(stop_port)[0]: print "存在pid, 正在执行命名杀死PID..." rets = get_Linux_status(stop_port)[1] for i in rets.split(): cmd = 'kill -9 %s'%i code,output1 = run_cmd(cmd) print "删除{} pid端口号为:{}".format(service_name,i) #stop_status(stop_port,time_num) stop_status_linux(stop_port,time_num) else: print "{}服务已停止".format(service_name) else: print "执行关闭命令失败退出" sys.exit(1) else: print "{} 服务未运行,请确认".format(service_name) sys.exit(1) if __name__ == "__main__": ##替换当前服务名称到service_name print Stop_Script_Path stop(user,Stop_Script_Path,service_name, stop_port,Stop_Script,time_num,common_stop_server)
上图是变量参数的入参