本篇和你們分享的是使用python簡化對jar包操做命令,封裝成簡短關鍵字或詞,達到操做簡便的目的。最近在回顧和構思shell腳本工具,後面一些文章應該會分享shell內容,但願你們繼續關注。java
這一步驟主要掃描指定磁盤中待啓動的jar包,而後獲取其路徑,方便後面操做java命令:python
1 #獲取磁盤中jar啓動包 2 def find_file_bypath(strDir): 3 filelist = os.listdir(strDir) 4 for file in filelist: 5 if os.path.isdir(strDir + "/" + file): 6 find_file_bypath(strDir + "/" + file) 7 else: 8 if(file.find(".jar") >= 0): 9 fileInfo = MoFileInfo(file,strDir + "/" + file) 10 all_list.append(fileInfo)
這個遞歸獲取路徑就很少說了,能夠參考前一篇文章linux
在linux中獲取某個程序pid並打印出來一般的命令是:shell
1 ps -ef | grep 程序名字
在py工具中一樣用到了grep命令,經過執行linux命令獲取相對應的pid值:app
1 #獲取pid 2 def get_pid(name): 3 child = subprocess.Popen(['pgrep', '-f', name], stdout=subprocess.PIPE, shell=False) 4 response = child.communicate()[0] 5 print(response) 6 return response
這裏直接取的第一個值,由於上面第一節已經可以定位到程序jar包的名字,因此獲取pid很容易工具
自定義其實就是用咱們隨便定義的單詞或關鍵字來代替jar包操做命令,這裏我封裝了有5種,分別以下:spa
{}表明的是pid和jar包全路徑,相關代碼:code
1 #執行命令 2 def exec_file(index): 3 try: 4 if(index <= -1): 5 pass 6 else: 7 fileInfo = all_list[int(index)] 8 print("你選擇的是:{}".format(fileInfo.path)) 9 strcmd = raw_input("請輸入執行命令(nr:nohup啓動java r:java啓動 k:kill d:刪除java包 kd:kill+刪除jar包):\r\n") 10 if(strcmd == "nr"): 11 os.system("nohup java -jar {} 2>&1 & ".format(fileInfo.path)) 12 elif(strcmd == "r"): 13 os.system("java -jar {}".format(fileInfo.path)) 14 elif(strcmd == "k"): 15 pid = get_pid(fileInfo.name) 16 print("pid:" + pid) 17 strcmd_1 = "kill -9 {}".format(pid) 18 exec_cmd(strcmd_1) 19 elif(strcmd == "d"): 20 strcmd_1 = "rm -rf {}".format(fileInfo.path) 21 exec_cmd(strcmd_1) 22 elif(strcmd == "kd"): 23 pid = get_pid(fileInfo.name) 24 strcmd_1 = "kill -9 {}".format(pid) 25 exec_cmd(strcmd_1) 26 27 strcmd_1 = "rm -rf {}".format(fileInfo.path) 28 exec_cmd(strcmd_1) 29 else: 30 print("無任何操做") 31 except: 32 print("操做失敗")
這裏python操做linux命令用到的方式是os.system(command),這樣已定保證了linux命令執行成功後才繼續下一步的操做;下面是本次分享內容的所有代碼:orm
1 #!/usr/bin/python 2 #coding=utf-8 3 import os 4 import subprocess 5 from subprocess import check_output 6 7 all_list = [] 8 9 class MoFileInfo: 10 def __init__(self,name,path): 11 self.name = name 12 self.path = path 13 14 #獲取磁盤中jar啓動包 15 def find_file_bypath(strDir): 16 filelist = os.listdir(strDir) 17 for file in filelist: 18 if os.path.isdir(strDir + "/" + file): 19 find_file_bypath(strDir + "/" + file) 20 else: 21 if(file.find(".jar") >= 0): 22 fileInfo = MoFileInfo(file,strDir + "/" + file) 23 all_list.append(fileInfo) 24 25 def show_list_file(): 26 for index,x in enumerate(all_list): 27 print("{}. {}".format(index,x.name)) 28 29 #獲取pid 30 def get_pid(name): 31 child = subprocess.Popen(['pgrep', '-f', name], stdout=subprocess.PIPE, shell=False) 32 response = child.communicate()[0] 33 print(response) 34 return response 35 36 #執行命令 37 def exec_file(index): 38 try: 39 if(index <= -1): 40 pass 41 else: 42 fileInfo = all_list[int(index)] 43 print("你選擇的是:{}".format(fileInfo.path)) 44 strcmd = raw_input("請輸入執行命令(nr:nohup啓動java r:java啓動 k:kill d:刪除java包 kd:kill+刪除jar包):\r\n") 45 if(strcmd == "nr"): 46 os.system("nohup java -jar {} 2>&1 & ".format(fileInfo.path)) 47 elif(strcmd == "r"): 48 os.system("java -jar {}".format(fileInfo.path)) 49 elif(strcmd == "k"): 50 pid = get_pid(fileInfo.name) 51 print("pid:" + pid) 52 strcmd_1 = "kill -9 {}".format(pid) 53 exec_cmd(strcmd_1) 54 elif(strcmd == "d"): 55 strcmd_1 = "rm -rf {}".format(fileInfo.path) 56 exec_cmd(strcmd_1) 57 elif(strcmd == "kd"): 58 pid = get_pid(fileInfo.name) 59 strcmd_1 = "kill -9 {}".format(pid) 60 exec_cmd(strcmd_1) 61 62 strcmd_1 = "rm -rf {}".format(fileInfo.path) 63 exec_cmd(strcmd_1) 64 else: 65 print("無任何操做") 66 except: 67 print("操做失敗") 68 69 def exec_cmd(strcmd): 70 str = raw_input("是否執行命令(y/n):" + strcmd + "\r\n") 71 if(str == "y"): 72 os.system(strcmd) 73 74 strDir = raw_input("請輸入jar所在磁盤路徑(默認:/root/job):\r\n") 75 strDir = strDir if (len(strDir) > 0) else "/root/job" 76 #獲取運行包 77 find_file_bypath(strDir) 78 #展現運行包 79 show_list_file() 80 #選擇運行包 81 strIndex = raw_input("請選擇要運行的編號:\r\n") 82 #執行命令 83 exec_file(strIndex)