用Python做出tomcat批量启动和关闭的工具
程序员文章站
2022-07-02 19:02:19
使用Python做一个tomcat批量启动和关闭的工具
前言
一台机器下有多个tomcat,希望用脚本批量启动和关闭而不是手动一个个启动关闭
代码:
# -*- co...
使用Python做一个tomcat批量启动和关闭的工具
前言
一台机器下有多个tomcat,希望用脚本批量启动和关闭而不是手动一个个启动关闭
代码:# -*- coding: utf-8 -*- import subprocess from sys import argv # 从命令行获取参数 method = argv[1] # tomcat名,对应路径中tomcat tomcats = ['tomcat-com-ar', 'tomcat-com-ftps', 'tomcat-com-mm', 'tomcat-devobject', 'tomcat-com-bm', 'tomcat-com-ir', 'tomcat-com-mq', 'tomcat-heninet', 'tomcat-com-er', 'tomcat-com-job', 'tomcat-com-um', 'tomcat-jenkins'] if method == "start": for tomcat in tomcats: res = subprocess.Popen("ps -ef | grep %s | grep -v grep | awk '{print $2}'" % tomcat.split('-', 1)[1], stdout=subprocess.PIPE, shell=True) toms = res.stdout.readlines() if len(toms) >= 1: print u"%s tomcat进程已启动!, pid为:%s\n" % (tomcat, toms[0]) continue print subprocess.Popen('/home/deploy/%s/bin/startup.sh' % tomcat, stdout=subprocess.PIPE,shell=True).communicate() print '\n' res = subprocess.Popen("ps -ef | grep %s | grep -v grep | awk '{print $2}'" % tomcat.split('-', 1)[1], stdout=subprocess.PIPE, shell=True) toms = res.stdout.readlines() if len(toms) == 0: print u"%s 启动失败,请检查后重试!\n" % tomcat elif len(toms) > 0: print u"%s 启动成功, pid为:%s\n" % (tomcat, toms[0]) elif method == "shutdown": for tomcat in tomcats: res = subprocess.Popen("ps -ef | grep %s | grep -v grep | awk '{print $2}'" % tomcat.split('-', 1)[1], stdout=subprocess.PIPE, shell=True) toms = res.stdout.readlines() if len(toms) < 1: print u"%s tomcat进程未启动!\n" % tomcat continue print u'正在关闭pid为的%s进程%s\n' % (toms[0], tomcat) print subprocess.Popen('/home/deploy/%s/bin/shutdown.sh' % tomcat, stdout=subprocess.PIPE, shell=True).communicate() print '\n' res = subprocess.Popen("ps -ef | grep %s | grep -v grep | awk '{print $2}'" % tomcat.split('-', 1)[1], stdout=subprocess.PIPE, shell=True) toms = res.stdout.readlines() if len(toms) > 0: print u"tomcat关闭失败,正在强制关闭\n" re = subprocess.Popen("kill -9 %s" % str(toms[0]), shell=True, stdout=subprocess.PIPE) print re.stdout.read() else: print u"命令参数错误\n"
上一篇: HTML5表单-form实战解析
下一篇: 笑喷,二货在那里,笑就在那里!