每天都要更新微服务, 写了个python脚本, 实现linux下一键启动jar包

每天都要更新微服务,  而且可能还要退回到上一个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)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值