启动JAVA服务

时间:2024-03-05 10:20:27
# -*- coding: utf-8 -*-
import re
import sys
from subprocess import Popen, PIPE
import os
import time
import commands


def getoutput(command):
    print \'start exec command:{}\'.format(command).center(20, \'-\')
    p = Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
    out, err = p.communicate()
    if err:
        print out
        print \'command exec error: [{}]\'.format(command)
        sys.exit(1)
    else:
        return out
        

def stop_java():
    cmd = "ps -ef | grep java | grep -v grep | awk \'{print $2}\' | xargs kill -9"  #注意 会杀死所有JAVA 进程 
    getoutput(cmd)
    print("运行java已停止")
    
def start_java(path_java,java_name):
    for i in java_name:
        start_command = "exec nohup java -jar %s 2>1& " %os.path.join(path_java, i)
        print(start_command)
        commands.getoutput(start_command)
        print("java已开启")
    
if __name__ == \'__main__\':
    path_java = \'/root/bpmaxx/\'  #java包的路径
    java_name = ["bpmaxx-eureka-1.0.0-SNAPSHOT.jar", "bpmaxx-config-1.0.0-SNAPSHOT.jar", "java -jar bpmaxx-gateway-1.0.0-SNAPSHOT.jar", "bpmaxx-maindata-1.0.0-SNAPSHOT.jar","bpmaxx-system-1.0.0-SNAPSHOT.jar"]  #需要启动的文件名
    stop_java() #
    time.sleep(3)
    start_java(path_java,java_name) # 开启