利用python腳本如何簡化jar操作命令
前言
本篇和大家分享的是使用python簡化對jar包操作命令,封裝成簡短關(guān)鍵字或詞,達到操作簡便的目的。最近在回顧和構(gòu)思shell腳本工具,后面一些文章應(yīng)該會分享shell內(nèi)容,希望大家繼續(xù)關(guān)注。
- 獲取磁盤中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 程序名字
在py工具中同樣用到了grep命令,通過執(zhí)行l(wèi)inux命令獲取相對應(yīng)的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
這里直接取的第一個值,因為上面第一節(jié)已經(jīng)能夠定位到程序jar包的名字,所以獲取pid很容易
自定義jar操作命令
自定義其實就是用我們隨便定義的單詞或關(guān)鍵字來代替jar包操作命令,這里我封裝了有5種,分別如下:
- nr:nohup java -jar {} 2>&1 &
- r:java -jar {}
- k:kill -9 {}
- d:rm -rf {}
- kd:kill -9 {}
{}代表的是pid和jar包全路徑,相關(guān)代碼:
#執(zhí)行命令
def exec_file(index):
try:
if(index <= -1):
pass
else:
fileInfo = all_list[int(index)]
print("你選擇的是:{}".format(fileInfo.path))
strcmd = raw_input("請輸入執(zhí)行命令(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命令執(zhí)行成功后才繼續(xù)下一步的操作;下面是本次分享內(nèi)容的全部代碼:
#!/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
#執(zhí)行命令
def exec_file(index):
try:
if(index <= -1):
pass
else:
fileInfo = all_list[int(index)]
print("你選擇的是:{}".format(fileInfo.path))
strcmd = raw_input("請輸入執(zhí)行命令(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("是否執(zhí)行命令(y/n):" + strcmd + "\r\n")
if(str == "y"):
os.system(strcmd)
strDir = raw_input("請輸入jar所在磁盤路徑(默認(rèn):/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")
#執(zhí)行命令
exec_file(strIndex)
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- Python爬蟲逆向分析某云音樂加密參數(shù)的實例分析
- Python調(diào)用jar包方法實現(xiàn)過程解析
- Python使用jpype模塊調(diào)用jar包過程解析
- Python代碼一鍵轉(zhuǎn)Jar包及Java調(diào)用Python新姿勢
- 用python解壓分析jar包實例
- python如何使用jt400.jar包代碼實例
- Python3 使用cookiejar管理cookie的方法
- python調(diào)用java的jar包方法
- Java實現(xiàn)的執(zhí)行python腳本工具類示例【使用jython.jar】
- 將Python代碼打包為jar軟件的簡單方法
- python 逆向爬蟲正確調(diào)用 JAR 加密邏輯
相關(guān)文章
python導(dǎo)出mysql指定binlog文件實現(xiàn)demo
這篇文章主要介紹了python導(dǎo)出mysql指定binlog文件實現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
python使用xmlrpclib模塊實現(xiàn)對百度google的ping功能
這篇文章主要介紹了python使用xmlrpclib模塊實現(xiàn)對百度google的ping功能,實例分析了xmlrpclib模塊的相關(guān)技巧,需要的朋友可以參考下2015-06-06
python cv2截取不規(guī)則區(qū)域圖片實例
今天小編就為大家分享一篇python cv2截取不規(guī)則區(qū)域圖片實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
wxPython繪圖模塊wxPyPlot實現(xiàn)數(shù)據(jù)可視化
這篇文章主要為大家詳細(xì)介紹了wxPython繪圖模塊wxPyPlot實現(xiàn)數(shù)據(jù)可視化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
Django Rest framework之權(quán)限的實現(xiàn)示例
這篇文章主要介紹了Django Rest framework之權(quán)限的實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12

