自动化启动服务

import os
import sys
import time
from _utils.patrol2 import run_cmd
import platform

def get_Linux_status(start_port):
    status_cmd = "netstat -anp |grep LISTEN | grep %s |awk '{print $7}' |awk -F'/' '{print $1}'| wc -l"%start_port
    port_cmd = "netstat -anp |grep LISTEN | grep %s |awk '{print $7}' |awk -F'/' '{print $1}'"  %start_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(start_port):
    cmd='netstat -ano | findstr {}'.format(start_port)
    print cmd
    doce,ret = run_cmd(cmd)if '%s' %(start_port) in ret:
        print "{}查询端口存在服务运行中...".format(start_port)
        output = False  #服务运行中  已开启 异常
    else:
        print "{}查询端存在...".format(start_port)
        output = True   #服务断开   未开启 正常 
        #sys.exit(1)
    return output



def start_status(start_port,time_num):
#循环环判断服务是否停止成功,当前设定3,理论上最多会出现30次    "服务正在停止......
    flag = 1
    while flag < time_num:
        if get_windows_port(start_port):
            print "{}服务正在启动...".format(service_name)
            flag+=1
            time.sleep(10)
        else:
            print "{}服务已运行......".format(service_name)
            break
    #print "{} 服务停止失败".format(service_name)
   # sys.exit(1)
    



'''*****************************
开启服务  路径文件的结合  例如:/bin/start.sh
*****************************'''
def start(user,start_Script_Path,service_name, start_port,start_Script,time_num,common_start_server):
    if sys.platform.startswith('win'):
        if get_windows_port(start_port):
            if common_start_server:
                cmd = "net start %s" %common_start_server
                code,output = run_cmd(cmd)
                if code == 0:
                    print " %s执行开启命令成功" %cmd
                    time.sleep(5)
                    start_status(start_port,time_num)   #循环环判断服务是否停止成功
                else:
                    print "执行开启命令失败退出"
                    sys.exit(1)
            else:
                os.chdir("%s" %start_Script_Path)
                start_cmd = '{}'.format(start_Script)
                print "执行开启服务:{}".format(start_cmd)
                code,output = run_cmd(start_cmd)
                if code == 0:
                    print "执行开启命令成功"
                    start_status(start_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(start_port)[0]:#   服务存在
            print "{} 服务已运行,请确认".format(service_name)
            sys.exit(1)
            
        else:
            print "服务已停止,开始执行开启服务"
            start_Script_Path_1 = os.path.join(start_Script_Path, start_Script)
            stop_cmd = "su - %s -c 'sh %s &' " %(user, start_Script_Path_1)
            print "执行开启服务:{}".format(stop_cmd)
            code,output = run_cmd(stop_cmd)
            if code == 0:
                print "执行开启命令成功"
                time.sleep(10)
                if get_Linux_status(start_port)[0]:
                    print "服务启动成功"
#                   print 
#                   print "存在pid, 正在执行命名杀死PID..."
#                   rets = get_Linux_status(start_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(start_port,time_num)
#                   if get_Linux_status(start_port)[0]:
#                       print "{}服务已停止".format(service_name)
#                   else:
#                       print "{}服务停止失败,请查看".format(service_name)
#                       sys.exit(1)
                else:
                   print "{}服务启动失败".format(service_name)
            else:
                print "执行关闭命令失败退出"
                sys.exit(1)
            


if __name__ == "__main__":
##替换当前服务名称到service_name
    print start_Script_Path
    start(user,start_Script_Path,service_name, start_port,start_Script,time_num,common_start_server)

启动的要求 和停止的要求一样  

 

 上述参数的输入

posted @ 2020-03-20 14:33  QQmini  阅读(214)  评论(0编辑  收藏  举报