python脚本简化jar操作命令

本篇和大家分享的是使用python简化对jar包操作命令,封装成简短关键字或词,达到操作简便的目的。最近在回顾和构思shell脚本工具,后面一些文章应该会分享shell内容,希望大家继续关注。

  • 获取磁盘中jar启动包
  • 获取某个程序进程pid
  • 自定义jar操作命令

获取磁盘中jar启动包

这一步骤主要扫描指定磁盘中待启动的jar包,然后获取其路径,方便后面操作java命令:

 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)

这个递归获取路径就不多说了,可以参考前一篇文章

获取某个程序进程pid

在linux中获取某个程序pid并打印出来通常的命令是:

1 ps -ef | grep 程序名字

在py工具中同样用到了grep命令,通过执行linux命令获取相对应的pid值:

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操作命令

自定义其实就是用我们随便定义的单词或关键字来代替jar包操作命令,这里我封装了有5种,分别如下:

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

{}代表的是pid和jar包全路径,相关代码:

 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命令执行成功后才继续下一步的操作;下面是本次分享内容的全部代码:

 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)

 

posted @ 2019-02-23 17:44  神牛003  阅读(1144)  评论(0编辑  收藏  举报