利用Python脚本简化Jar包启动、停止与删除操作
本文介绍如何使用Python脚本封装Linux下的Jar包操作命令,通过自定义关键字实现一键启动、停止和删除。脚本支持递归扫描指定目录下的Jar文件,利用pgrep获取进程PID,并结合os.system执行nohup、kill及rm命令,旨在简化日常运维中的繁琐操作。
前言
本文分享一种使用Python脚本简化Jar包操作命令的方法,将复杂的操作封装为简短的关键字或词汇,从而实现操作的便捷性。近期正在回顾和构思Shell脚本工具,后续文章可能会涉及Shell相关内容,敬请持续关注。
- 获取磁盘中Jar启动包
- 获取某个程序的进程PID
- 自定义Jar操作命令
获取磁盘中Jar启动包
此步骤主要扫描指定磁盘路径下待启动的Jar包,并获取其完整路径,以便后续执行Java命令:
#获取磁盘中jar启动包
def find_file_bypath(strDir):
filelist = os.listdir(strDir)
for file in filelist:
if os.path.isdir(strDir + "/" + file):
find_file_bypath(strDir + "/" + file)
else:
if(file.find(".jar") >= 0):
fileInfo = MoFileInfo(file,strDir + "/" + file)
all_list.append(fileInfo)
关于递归获取路径的具体实现,此处不再赘述,可参考前文介绍。
获取某个程序的进程PID
在Linux系统中,获取某个程序PID并打印出来的常用命令是:
1 ps -ef | grep 程序名字
在Python工具中,同样调用了grep命令,通过执行Linux命令来获取对应的PID值:
#获取pid def get_pid(name): child = subprocess.Popen(['pgrep', '-f', name], stdout=subprocess.PIPE, shell=False) response = child.communicate()[0] print(response) return response
这里直接取第一个返回值,因为上一节已经能够定位到程序Jar包的名字,所以获取PID相对容易。
自定义Jar操作命令
自定义操作即使用自定义的单词或关键字来替代Jar包的操作命令。这里封装了5种命令,具体如下:
- nr:nohup java -jar {} 2>&1 &
- r:java -jar {}
- k:kill -9 {}
- d:rm -rf {}
- kd:kill -9 {}
{}代表的是PID和Jar包的全路径,相关代码如下:
#执行命令
def exec_file(index):
try:
if(index <= -1):
pass
else:
fileInfo = all_list[int(index)]
print("你选择的是:{}".format(fileInfo.path))
strcmd = raw_input("请输入执行命令(nr:nohup启动java r:java启动 k:kill d:删除java包 kd:kill+删除jar包):\r\n")
if(strcmd == "nr"):
os.system("nohup java -jar {} 2>&1 & ".format(fileInfo.path))
elif(strcmd == "r"):
os.system("java -jar {}".format(fileInfo.path))
elif(strcmd == "k"):
pid = get_pid(fileInfo.name)
print("pid:" + pid)
strcmd_1 = "kill -9 {}".format(pid)
exec_cmd(strcmd_1)
elif(strcmd == "d"):
strcmd_1 = "rm -rf {}".format(fileInfo.path)
exec_cmd(strcmd_1)
elif(strcmd == "kd"):
pid = get_pid(fileInfo.name)
strcmd_1 = "kill -9 {}".format(pid)
exec_cmd(strcmd_1)
strcmd_1 = "rm -rf {}".format(fileInfo.path)
exec_cmd(strcmd_1)
else:
print("无任何操作")
except:
print("操作失败")
这里Python操作Linux命令采用的方式是os.system(command) ,这样确保了在Linux命令执行成功后才继续下一步操作;以下是本次分享内容的完整代码:
#!/usr/bin/python
#coding=utf-8
import os
import subprocess
from subprocess import check_output
all_list = []
class MoFileInfo:
def __init__(self,name,path):
self.name = name
self.path = path
#获取磁盘中jar启动包
def find_file_bypath(strDir):
filelist = os.listdir(strDir)
for file in filelist:
if os.path.isdir(strDir + "/" + file):
find_file_bypath(strDir + "/" + file)
else:
if(file.find(".jar") >= 0):
fileInfo = MoFileInfo(file,strDir + "/" + file)
all_list.append(fileInfo)
def show_list_file():
for index,x in enumerate(all_list):
print("{}. {}".format(index,x.name))
#获取pid
def get_pid(name):
child = subprocess.Popen(['pgrep', '-f', name], stdout=subprocess.PIPE, shell=False)
response = child.communicate()[0]
print(response)
return response
#执行命令
def exec_file(index):
try:
if(index <= -1):
pass
else:
fileInfo = all_list[int(index)]
print("你选择的是:{}".format(fileInfo.path))
strcmd = raw_input("请输入执行命令(nr:nohup启动java r:java启动 k:kill d:删除java包 kd:kill+删除jar包):\r\n")
if(strcmd == "nr"):
os.system("nohup java -jar {} 2>&1 & ".format(fileInfo.path))
elif(strcmd == "r"):
os.system("java -jar {}".format(fileInfo.path))
elif(strcmd == "k"):
pid = get_pid(fileInfo.name)
print("pid:" + pid)
strcmd_1 = "kill -9 {}".format(pid)
exec_cmd(strcmd_1)
elif(strcmd == "d"):
strcmd_1 = "rm -rf {}".format(fileInfo.path)
exec_cmd(strcmd_1)
elif(strcmd == "kd"):
pid = get_pid(fileInfo.name)
strcmd_1 = "kill -9 {}".format(pid)
exec_cmd(strcmd_1)
strcmd_1 = "rm -rf {}".format(fileInfo.path)
exec_cmd(strcmd_1)
else:
print("无任何操作")
except:
print("操作失败")
def exec_cmd(strcmd):
str = raw_input("是否执行命令(y/n):" + strcmd + "\r\n")
if(str == "y"):
os.system(strcmd)
strDir = raw_input("请输入jar所在磁盘路径(默认:/root/job):\r\n")
strDir = strDir if (len(strDir) > 0) else "/root/job"
#获取运行包
find_file_bypath(strDir)
#展示运行包
show_list_file()
#选择运行包
strIndex = raw_input("请选择要运行的编号:\r\n")
#执行命令
exec_file(strIndex)
总结
以上就是本文的全部内容,希望本文的内容对大家的学习或工作具有一定的参考借鉴价值。如果有疑问,欢迎留言交流,感谢大家对本站的支持。
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













