# import os
# os.system('lsof -i:80')
# 如何使用 :
# 2021年9月14日11:11:00 更新
# kill_java_and_nohup.py 放在 jar包所在目录
# whereis python 查到python的路径
# 因为linux 一般是默认装的python2 而我这个是python3的
# 所以要确保 linux上有python3 ,假如查到一个python3.6
# 他是在 /usr/bin/python3.6
# 那就直接 python3.6 kill_java_and_nohup.py
# 如果python装的位置不是在bin 底下的
# 那就直接绝对路径
# 比如说python在 /home/aaa/python3.6
# 那么 /home/aaa/python3.6 kill_java_and_nohup.py
# 我这里jar包名字写死了 demo-0.0.1-SNAPSHOT.jar
# 端口写死了. 需要自己换
# 会有 kill 不掉 java的问题 这样很麻烦
# 很多次都是第一次 kill 不掉,第二次才能kill掉。。
import os
import time
class Config():
def __init__(self):
self.jar_name = "demo-0.0.1-SNAPSHOT.jar"
self.log_file_name_templete="log_{}.log"
self.port="80"
def show_info(out):
info = out.readlines()
for line in info: # 按行遍历
line = line.strip('\r\n')
print(line)
def kill_java(config):
# port = "80"
port = config.port
# command = 'ping www.baidu.com' #可以直接在命令行中执行的命令
r = os.popen('lsof -i:' + port)
# r = os.popen(command) #执行该命令
info = r.readlines() # 读取命令行的输出到一个list
have_java = False
for line in info: # 按行遍历
line = line.strip('\r\n')
# print(line)
if line.startswith("java"):
kill_java_line(line)
return False
# java 杀完了
return True
def kill_java_and_nohup(config):
# port="80"
# jar_name="demo-0.0.1-SNAPSHOT.jar"
# # command = 'ping www.baidu.com' #可以直接在命令行中执行的命令
# r = os.popen('lsof -i:'+port)
# # r = os.popen(command) #执行该命令
# info = r.readlines() #读取命令行的输出到一个list
# for line in info: #按行遍历
# line = line.strip('\r\n')
# print(line)
# if line.startswith("java"):
# kill_java_line(line)
# while kill_java():
# # kill_java()
# time.sleep(1)
# kill_java()
kill_java(config)
# jar_name = "demo-0.0.1-SNAPSHOT.jar"
jar_name = config.jar_name
print("nohup java -jar")
# log_version=11
# with open("log_version.txt","r") as f:
# data=f.read().strip()
# log_version=int(data)
# log_file_name="kinect85_0.0.1_redpack10.log"
time_str = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime())
# print(time_str)
# log_file_name = "kinect0.0.1_redpack{}.log".format(time_str)
log_file_name = config.log_file_name_templete.format(time_str)
# os.system("nohup java -jar demo-0.0.1-SNAPSHOT.jar > "+log_file_name+" &")
# out=os.popen("nohup java -jar demo-0.0.1-SNAPSHOT.jar > "+log_file_name+" &")
out = os.popen("nohup java -jar " + jar_name + " > " + log_file_name + " &")
# os.system("")
# out.readlines()
show_info(out)
# print(out)
# print("log_file_name: ", log_file_name)
print("cmd : cat ",log_file_name)
# with open("log_version.txt","w") as f:
# f.write(str(log_version+1))
# cat kinect85_0.0.1_redpack8.log
def remove(lst, what=""):
ret = []
for val in lst:
if val == what:
continue
ret.append(val)
return ret
def kill_java_line(line):
print("kill_java_line")
# sps=line.split(" ")
# pid=sps[1]
sps = line.split(" ")
# sps.remove("")
sps = remove(sps, "")
# print(sps)
pid = sps[1]
# os.system('kill '+pid)
kill_pid = 'kill ' + pid
print("kill_pid: ", kill_pid)
out = os.popen(kill_pid)
# print(out)
# show_info(out)
def create_version_txt():
# version_txt_path="test_file.txt"
version_txt_path = "log_version.txt"
if not os.path.exists(version_txt_path):
print("not exists", version_txt_path)
with open(version_txt_path, "w") as f:
data = str(1)
f.write(data)
# create_version_txt()
config=Config()
kill_java_and_nohup(config)
jar包 nohup 脚本
最新推荐文章于 2024-04-23 23:23:57 发布