由于微服务比较多 ,为了每次省事方便,写成了脚本启动,把所有的微服务jar包集中写成启动脚本,代码如下:
#!/usr/bin/env python3
import os
import sys, subprocess
import time
class Startup(object):
def __init__(self, ServerName, Pid):
self.ServerName = ServerName
self.Pid = Pid
def start(self, cmd):
pid = subprocess.getstatusoutput(self.Pid)
if not pid[-1]:
os.system(cmd)
else:
print('%s is already running.pid=%s' % (self.ServerName, pid))
def stop(self):
while True:
pid = subprocess.getstatusoutput(self.Pid)
if pid[-1]:
os.system('kill -9 %s' % pid[-1])
time.sleep(10)
else:
print('%s is not running' % self.ServerName)
break
def status(self):
pid = subprocess.getstatusoutput(self.Pid)
if pid[-1]:
print('%s is already running.pid=%s' % (self.ServerName, pid))
else:
print('%s is Not Running' % self.ServerName)
if __name__ == '__main__':
# 修改对应自己的java包服务
service_dict = {
'1': 'xxxx01.jar',
'2': 'xxxx02.jar',
'3': 'xxxx03.jar'
}
service_str = '------请选择下面数字对应的服务-------\n'
for s in str(service_dict).strip('{}').split(','):
service_str += s.strip() + '\t\n'
print(service_str)
# 暂时支持3个,如超过3个自行添加
num = input("请输入jar服务对应的数字:")
if num == '1':
ServerName = service_dict.get(num)
start_cmd = 'java -jar /data/01/%s >/data/01/nohup.out 2>&1 &' % ServerName
elif num == '2':
ServerName = service_dict.get(num)
start_cmd = 'java -jar /data/02/%s >/data/02/nohup.out 2>&1 &' % ServerName
elif num == '3':
ServerName = service_dict.get(num)
start_cmd = 'java -jar /data/03/%s >/data/03/nohup.out 2>&1 &' % ServerName
else:
print('输入错误!请输入服务的正确数字!!!')
sys.exit()
Pid = "ps -ef|grep %s |grep -v grep |awk '{print $2}'" % ServerName
startup = Startup(ServerName, Pid)
try:
if sys.argv[1] == 'start':
startup.start(start_cmd)
elif sys.argv[1] == 'stop':
startup.stop()
elif sys.argv[1] == 'restart':
startup.stop()
startup.start(start_cmd)
elif sys.argv[1] == 'status':
startup.status()
except Exception:
print("Usage: py xxx.py [start|stop|restart|status]")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)