利用python腳本如何簡化jar操作命令
前言
本篇和大家分享的是使用python簡化對jar包操作命令,封裝成簡短關(guān)鍵字或詞,達(dá)到操作簡便的目的。最近在回顧和構(gòu)思shell腳本工具,后面一些文章應(yīng)該會分享shell內(nèi)容,希望大家繼續(xù)關(guān)注。
- 獲取磁盤中jar啟動包
- 獲取某個程序進(jìn)程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)
這個遞歸獲取路徑就不多說了,可以參考前一篇文章
獲取某個程序進(jìn)程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操作命令
自定義其實(shí)就是用我們隨便定義的單詞或關(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"
#獲取運(yùn)行包
find_file_bypath(strDir)
#展示運(yùn)行包
show_list_file()
#選擇運(yùn)行包
strIndex = raw_input("請選擇要運(yùn)行的編號:\r\n")
#執(zhí)行命令
exec_file(strIndex)
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- Python爬蟲逆向分析某云音樂加密參數(shù)的實(shí)例分析
- Python調(diào)用jar包方法實(shí)現(xiàn)過程解析
- Python使用jpype模塊調(diào)用jar包過程解析
- Python代碼一鍵轉(zhuǎn)Jar包及Java調(diào)用Python新姿勢
- 用python解壓分析jar包實(shí)例
- python如何使用jt400.jar包代碼實(shí)例
- Python3 使用cookiejar管理cookie的方法
- python調(diào)用java的jar包方法
- Java實(shí)現(xiàn)的執(zhí)行python腳本工具類示例【使用jython.jar】
- 將Python代碼打包為jar軟件的簡單方法
- python 逆向爬蟲正確調(diào)用 JAR 加密邏輯
相關(guān)文章
Python 常用日期處理 -- calendar 與 dateutil 模塊的使用
這篇文章主要介紹了Python如何使用calendar 與 dateutil 模塊處理日期,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-09-09
Python面向?qū)ο髮?shí)現(xiàn)數(shù)據(jù)分析的實(shí)例詳解
這篇文章主要通過幾個實(shí)例為大家詳細(xì)介紹了Python面向?qū)ο髮?shí)現(xiàn)數(shù)據(jù)分析的方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下2023-01-01
使用pymysql查詢數(shù)據(jù)庫,把結(jié)果保存為列表并獲取指定元素下標(biāo)實(shí)例
這篇文章主要介紹了使用pymysql查詢數(shù)據(jù)庫,把結(jié)果保存為列表并獲取指定元素下標(biāo)實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
pytorch 利用lstm做mnist手寫數(shù)字識別分類的實(shí)例
今天小編就為大家分享一篇pytorch 利用lstm做mnist手寫數(shù)字識別分類的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

