每天都要更新微服务, 而且可能还要退回到上一个jar包, 就写了个python脚本, 一键启动jar包, 括杀死进程, 启动参数,
只需设置 # sDir = 程序目录名, sName = jar包前缀, ymlName = 配置文件, out = 日志, sDtVer = 文件格式
1. 设置的参数 yml文件
2. jar文件 *-yyyymmddhhmmss.jar
参数启动写死了, 诸如一些判断还没有写进来, 需要完善
通过sheel启动就可以了
#/bin/bash
python go2.py
go.py脚本如下, 启动最新修改的jar文件
import sys
import os
import re
import time
import subprocess
print "waht-app-jar信息"
# 把jar包, yml, log文件夹 放在工程目录
# sDir = 程序目录名, sName = jar包, ymlName = 配置文件, out = 日志
sName = 'wqht-hl-1.1-SNAPSHOT'
ymlName = ""
ymlName = "application-release.yml"
sDir = "/srv/wqht-app-hl"
outName = "log-"+time.strftime("%Y%m%d-%H%M%S", time.localtime()) +".out"
logPath = sDir + "/log"
isExists = os.path.exists(logPath)
if not isExists: #判断结果
os.makedirs(logPath)
sline = '................................................................................'
print('\033[1;32m' + sline+' \033[0m')
print("查看进程:" + '\033[1;32m' + sName+' \033[0m')
cmdProc = "ps -ef | grep " + sName + " | grep -v grep"
cmdKill = 'ps -ef | grep ' + sName + ' | grep -v grep | awk \'{print "kill "$2}\' | sh'
out1 = 'ps -ef '
#print('\033[1;95m' + "kill:" + sName + ' \033[0m')
#print('\033[1;35mKILL: ' + sName +'\033[0m')
#先杀它3次
for i in range(1, 3):
#print(cmdProc)
out1,out2 = subprocess.Popen(cmdProc, shell=True, stdout=subprocess.PIPE).communicate()
if out1 != '' : #
print("\033[1;35m尝试第" + str(i) + "次杀死进程:\033[0m" + out1)
subprocess.Popen(cmdKill, shell=True, stdout=subprocess.PIPE)
time.sleep(2)
else :
print('\033[1;32m就绪...\033[0m')
time.sleep(1)
break
out1,out2 = subprocess.Popen(cmdProc, shell=True, stdout=subprocess.PIPE).communicate()
if out1 != '' : #
print("\033[1;35m进程冥顽不灵:\033[0m" + out1 + " 枪毙5s!!!")
cmdKill = 'ps -ef | grep ' + sName + ' | grep -v grep | awk \'{print "kill -9 "$2}\' | sh'
subprocess.Popen(cmdKill, shell=True, stdout=subprocess.PIPE)
time.sleep(5)
#获取最新创建的jar文件
cc = []
dd = {}
for filename in os.listdir(sDir):
if not filename.endswith('.jar'):
continue
path = sDir +"/" + filename
t1 = os.path.getmtime(path) #修改时间
kk = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(t1))
dd[kk] = filename
cc.append(kk)
lastName = sName + ".jar"
for i in sorted(cc):
lastName = dd[i]
print('jar文件:'+'\033[1;32m' + lastName + '\033[0m')
cmdJar = "java -jar ./" + lastName + " --spring.config.location=./" + ymlName
cmdJar = "nohup " + cmdJar + " >>./o.out 2>&1 &"
cmdJar = "cd " + sDir + ";" + cmdJar;
print('----------------------------------------------------------------------')
print(cmdJar)
cmdCP = "mv " + sDir + "/o.out " + sDir + "/log/" + outName
subprocess.Popen(cmdCP, shell=True, stdout=subprocess.PIPE)
out1 = subprocess.Popen(cmdJar, shell=True, close_fds=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
go2.py脚本如下, 启动按日期格式的jar文件
import sys
import os
import re
import time
import subprocess
print "app-jar信息"
# 把jar包, yml, log文件夹 放在工程目录
# sDir = 程序目录名, sName = jar包前缀, ymlName = 配置文件, out = 日志, sDtVer = yyyymmddhhmmss jar包安日期排版
sName = 'hl-1.1-SNAPSHOT'
ymlName = ""
ymlName = "application-release.yml"
sDir = "/srv/app-hl"
outName = "log-"+time.strftime("%Y%m%d%H%M%S", time.localtime()) +".out"
sDtVer = "([2][0]\d{2}[01]\d{1}\d{2}\d{6})?"
sDtVer = '([2][0]\d{6}\d{6})?'
# other
sline = '................................................................................'
print('\033[1;32m' + sline+' \033[0m')
print("查看进程:" + '\033[1;32m' + sName+' \033[0m')
cmdProc = "ps -ef | grep " + sName + " | grep -v grep"
cmdKill = 'ps -ef | grep ' + sName + ' | grep -v grep | awk \'{print "kill "$2}\' | sh'
out1 = 'ps -ef '
#print('\033[1;95m' + "kill:" + sName + ' \033[0m')
#print('\033[1;35mKILL: ' + sName +'\033[0m')
#先杀它3次
for i in range(1, 3):
out1,out2 = subprocess.Popen(cmdProc, shell=True, stdout=subprocess.PIPE).communicate()
if out1 != '' : #
print("\033[1;35m尝试第" + str(i) + "次杀死进程:\033[0m" + out1)
subprocess.Popen(cmdKill, shell=True, stdout=subprocess.PIPE)
time.sleep(2)
else :
print('\033[1;32m就绪...\033[0m')
time.sleep(1)
break
out1,out2 = subprocess.Popen(cmdProc, shell=True, stdout=subprocess.PIPE).communicate()
if out1 != '' : #
print("\033[1;35m进程冥顽不灵:\033[0m" + out1 + " 枪毙5s!!!")
cmdKill = 'ps -ef | grep ' + sName + ' | grep -v grep | awk \'{print "kill -9 "$2}\' | sh'
subprocess.Popen(cmdKill, shell=True, stdout=subprocess.PIPE)
time.sleep(5)
#获取最新jar文件
def file_name(file_dir):
for root, dirs, files in os.walk(file_dir):
#print(root) #当前目录路径
#print(dirs) #当前路径下所有子目录
print(files) #当前路径下所有非目录子文件
#pattern = re.compile(r'web-1.0-SNAPSHOT\s([2][0]\d{2}[01]\d{1}\d{2})?')
pattern = re.compile(sName + sDtVer)
dir_list = os.listdir(file_dir)
filenames=os.listdir(file_dir)
filenames.sort()
for fileName in filenames:
if not re.match(pattern, fileName) is None:
lastName = fileName
print (fileName)
print "lastName:" + lastName
jarName = lastName
#bicycles = sDir +';./' + jarName +' ;./' + ymlName + ' ;./' + outName + ' ;';';
result = sDir +';'+ sName + ';' + './'+jarName +';'
if len(ymlName) > 0:
result += " --spring.config.location=./" + ymlName +";"
else:
result += " ;"
if len(outName) > 0 :
result += " > ./log/" + outName + " 2>&1 & ;"
else:
result += " ;"
result = jarName
return result
rs = file_name(sDir)
cmdJar = "java -jar ./" + rs + " --spring.config.location=./" + ymlName
carJar = "nohup " + cmdJar + " >>./" + outName + " 2>&1 &"
cmdJar = "cd " + sDir + ";" + cmdJar;
print(cmdJar)
out1 = subprocess.Popen(cmdJar, shell=True, close_fds=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
本文介绍了一种使用Python脚本实现微服务一键启动和更新的方法,包括杀死旧进程、启动新服务及日志管理。脚本能自动查找并运行最新的jar包,支持配置文件加载和日志文件滚动。

被折叠的 条评论
为什么被折叠?



