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

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

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

前言

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

這里直接取的第一個值,因?yàn)樯厦娴谝还?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í)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

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

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

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

    python導(dǎo)出mysql指定binlog文件實(shí)現(xiàn)demo

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

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

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

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

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

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

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

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

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

    python爬取酷狗音樂Top500榜單

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

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

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

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

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

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

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

最新評論

襄汾县| 新乡市| 无极县| 日照市| 怀宁县| 嘉兴市| 博白县| 兴义市| 浏阳市| 民勤县| 延庆县| 原阳县| 开鲁县| 卢湾区| 辽中县| 黄龙县| 澜沧| 化德县| 积石山| 会泽县| 化州市| 陆丰市| 通化县| 土默特左旗| 祁东县| 神农架林区| 中方县| 利津县| 旬阳县| 长兴县| 墨脱县| 东乡县| 海城市| 重庆市| 乌拉特中旗| 河东区| 瑞金市| 兰坪| 庐江县| 西林县| 五峰|