利用python腳本如何簡化jar操作命令

這篇文章主要給大家介紹了關於利用python腳本如何簡化jar操作命令的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

前言

本篇和大家分享的是使用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 程序名字

在py工具中同樣用到了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)

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對神馬文庫的支持。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章