最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2019年02月24日 10:26:01   作者:神牛003  
這篇文章主要給大家介紹了關于利用python腳本如何簡化jar操作命令的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

前言

本篇和大家分享的是使用python簡化對jar包操作命令,封裝成簡短關鍵字或詞,達到操作簡便的目的。最近在回顧和構思shell腳本工具,后面一些文章應該會分享shell內容,希望大家繼續(xù)關注。

  • 獲取磁盤中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命令獲取相對應的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操作命令

自定義其實就是用我們隨便定義的單詞或關鍵字來代替jar包操作命令,這里我封裝了有5種,分別如下:

  • nr:nohup java -jar {} 2>&1 &
  • r:java -jar {}
  • k:kill -9 {}
  • d:rm -rf {}
  • kd:kill -9 {}

{}代表的是pid和jar包全路徑,相關代碼:

#執(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ù)下一步的操作;下面是本次分享內容的全部代碼:

#!/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所在磁盤路徑(默認:/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)

總結

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

相關文章

  • Python3中處理和操作純文本文件的詳細教程

    Python3中處理和操作純文本文件的詳細教程

    本教程將簡要描述 Python 能夠處理的一些文件格式,在簡要介紹這些文件格式之后,你將學習如何在 Python 3 中打開、讀取和寫入文本文件,完成后,你將能夠處理 Python 中的任何純文本文件,需要的朋友可以參考下
    2024-06-06
  • python導出mysql指定binlog文件實現(xiàn)demo

    python導出mysql指定binlog文件實現(xiàn)demo

    這篇文章主要介紹了python導出mysql指定binlog文件實現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • python使用xmlrpclib模塊實現(xiàn)對百度google的ping功能

    python使用xmlrpclib模塊實現(xiàn)對百度google的ping功能

    這篇文章主要介紹了python使用xmlrpclib模塊實現(xiàn)對百度google的ping功能,實例分析了xmlrpclib模塊的相關技巧,需要的朋友可以參考下
    2015-06-06
  • python cv2截取不規(guī)則區(qū)域圖片實例

    python cv2截取不規(guī)則區(qū)域圖片實例

    今天小編就為大家分享一篇python cv2截取不規(guī)則區(qū)域圖片實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • wxPython繪圖模塊wxPyPlot實現(xiàn)數(shù)據(jù)可視化

    wxPython繪圖模塊wxPyPlot實現(xiàn)數(shù)據(jù)可視化

    這篇文章主要為大家詳細介紹了wxPython繪圖模塊wxPyPlot實現(xiàn)數(shù)據(jù)可視化,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Python 圖像處理之PIL庫詳解用法

    Python 圖像處理之PIL庫詳解用法

    對于圖像識別,大量的工作在于圖像的處理,處理效果好,那么才能很好地識別,因此,良好的圖像處理是識別的基礎。在Python中,有一個優(yōu)秀的圖像處理框架,就是PIL庫,本文會介紹PIL庫中的各種方法,并列舉相關例子
    2021-11-11
  • python爬取酷狗音樂Top500榜單

    python爬取酷狗音樂Top500榜單

    大家好,本篇文章主要講的是python爬取酷狗音樂Top500榜單,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • pytorch加載自己的圖像數(shù)據(jù)集實例

    pytorch加載自己的圖像數(shù)據(jù)集實例

    這篇文章主要介紹了pytorch加載自己的圖像數(shù)據(jù)集實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • python與xml數(shù)據(jù)的交互詳解

    python與xml數(shù)據(jù)的交互詳解

    這篇文章主要介紹了python與xml數(shù)據(jù)的交互詳解,文章圍繞主題站卡詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-07-07
  • Django Rest framework之權限的實現(xiàn)示例

    Django Rest framework之權限的實現(xiàn)示例

    這篇文章主要介紹了Django Rest framework之權限的實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12

最新評論

巴彦县| 重庆市| 龙川县| 乌鲁木齐市| 怀化市| 吴江市| 余庆县| 阿瓦提县| 寿阳县| 班玛县| 宁南县| 和龙市| 中方县| 焉耆| 乐平市| 勐海县| 宁城县| 中阳县| 云霄县| 南昌市| 洱源县| 延长县| 剑川县| 珠海市| 乌拉特中旗| 天镇县| 多伦县| 曲水县| 淅川县| 永定县| 卢龙县| 嵩明县| 当阳市| 额敏县| 横山县| 莲花县| 东乡族自治县| 五指山市| 齐齐哈尔市| 修武县| 巨鹿县|