脚本用途:重启jar进程,需要传入jar名, 如hello
注意事项:
- jar名不要传入版本号,也不要加入通配符*。
运行命令: python restart-jar.py jar_file
restart-jar.py文件
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -*- version: v0.1 -*-
# 脚本用途:重启jar,需要传入jar名, 如hell
# 注意事项:
# jar名不要传入版本号,也不要加入通配符*。
#
# 运行命令: python restart-jar.py jar_file
import os
import sys
import subprocess
def run_cmd(cmd):
"""
运行单条命令
:return:
"""
print("cmd: " + cmd)
return os.popen(cmd).readlines()
def async_cmd(cmd):
"""
子进程运行命令
"""
if __name__ == "__main__":
if len(sys.argv) <= 1:
print('请输入程序名!')
else:
"""
sys.argv[1]: jar_name
"""
jar_name = sys.argv[1] + "*"
print("kill进程:" + jar_name)
grep_pid_cmd = "ps -ef | grep " + jar_name + " | grep -vE '(grep|restart-jar)' | awk '{print $2}'"
pids = run_cmd(grep_pid_cmd)
# 关闭进程
if pids:
for pid in pids:
print("正在kill进程,进程id:" + pid)
kill_pid_cmd = "kill " + pid
run_cmd(kill_pid_cmd)
else:
print("没有进程在运行。")
# 删除nohup
nohup_path = os.path.join(sys.path[0], "nohup.out")
rm_cmd = "rm -f " + nohup_path
print("删除文件: " + nohup_path)
run_cmd(rm_cmd)
# 启动进程
print("正在启动服务:" + jar_name)
start_cmd = "nohup java -jar " + jar_name + " &> " + nohup_path + " &"
print("启动命令:" + start_cmd)
try:
res = subprocess.check_call([start_cmd], shell=True)
except BaseException:
print("启动jar失败")
else:
print("启动结果:" + str(res))
本文作者: seawish
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!
网友评论