Python Tomcat Script(多实例)

之前书写过 Tomcat 单实例的 Python 脚本,本次增加 Tomcat 多实例的操作脚本.

1:准备

  安装所需 Python 插件
    A方法: pip install argparse

    B方法:1.安装依赖包{setuptools}

       2.安装 argparse

#tar xzf setuptools-19.1.1.tar.gz && cd setuptools-19.1.1 && python setup.py install
#tar xzf argparse-1.4.0.tar.gz && cd argparse-1.4.0 && python setup.py install
View Code

2.脚本内容

  1 #!/usr/bin/env python
  2 # _*_coding:utf-8_*_
  3 # Author "Edward.Liu"
  4 
  5 # Import libary~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6 import subprocess
  7 import time
  8 import sys
  9 import signal
 10 import os
 11 import argparse
 12 
 13 
 14 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 15 class Tomcat(object):
 16     def __init__(self, tomcat_exe):
 17         self.tomcat_exe = tomcat_exe
 18         self.Tomcat_Home = "/install/%s" % tomcat_exe
 19         self.Tomcat_Log_Home = "/install/%s/logs" % tomcat_exe
 20         self.counnt = 10
 21         self.Tomcat_Examples = ['tomcat', 'tomcat-mobile']
 22 
 23     # Get Tomcat_PID~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 24     def get_tomcat_pid(self):
 25         p = subprocess.Popen(['ps', '-Ao', 'pid,command'], stdout=subprocess.PIPE)
 26         out, err = p.communicate()
 27         for line in out.splitlines():
 28             if 'java' in line:
 29                 if self.tomcat_exe in line:
 30                     pid = int(line.split(None, 1)[0])
 31                     return pid
 32 
 33     # Start Tomcat Process~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 34     def start_tomcat(self):
 35         if self.get_tomcat_pid() is not None:
 36             print "33[32m %s Is Started 33[0m" % self.tomcat_exe
 37         else:
 38             # Start Tomcat
 39             command_start_tomcat = "%s/bin/startup.sh" % self.Tomcat_Home
 40             p = subprocess.Popen(command_start_tomcat, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
 41                                  stderr=subprocess.PIPE, shell=True)
 42             stdout, stderr = p.communicate()
 43             print stdout, stderr
 44 
 45     # Stop Tomcat process~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 46     def stop_tomcat(self):
 47         wait_sleep = 0
 48         if self.get_tomcat_pid() is None:
 49             print "33[32m %s is Not Running33[0m" % self.tomcat_exe + "~" * 20
 50         else:
 51             command_stop_tomcat = "%s/bin/shutdown.sh" % self.Tomcat_Home
 52             p = subprocess.Popen(command_stop_tomcat, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
 53                                  stderr=subprocess.PIPE, shell=True)
 54             stdout, stderr = p.communicate()
 55             while (self.get_tomcat_pid() is not None):
 56                 print "waiting for processes to exit
"
 57                 wait_sleep += 1
 58                 time.sleep(1)
 59                 if wait_sleep == self.counnt:
 60                     os.kill(self.get_tomcat_pid(), signal.SIGKILL)
 61                     print "33[32m Stop Tomcat is sucessful 33[0m"
 62                     break
 63 
 64     # View TomcatLogs~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 65     def tomcat_log(self):
 66         command_tomcat_log = "tail -f %s/catalina.out " % self.Tomcat_Log_Home
 67         p = subprocess.Popen(command_tomcat_log, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
 68         returncode = p.poll()
 69         try:
 70             while returncode is None:
 71                 line = p.stdout.readline()
 72                 returncode = p.poll()
 73                 line = line.strip()
 74                 print line
 75             print returncode
 76         except KeyboardInterrupt:
 77             print 'ctrl+d or z'
 78 
 79 
 80 if __name__ == '__main__':
 81     parser = argparse.ArgumentParser(
 82             description="eg: '%(prog)s' -c tomcat|tomcat-mobile -d {start|stop|status|restart|log}")
 83     # ADD Tomcat Apps ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 84     parser.add_argument('-c', '--app_name', nargs='+', dest='choices', choices=('tomcat', 'tomcat-mobile'))
 85     parser.add_argument('-d', '--Handle', action='store', nargs='?', dest='handle',
 86                         help='Input One of the {start|stop|status|restart|log}')
 87     parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0')
 88     args = parser.parse_args()
 89     if len(sys.argv) <= 4:
 90         parser.print_help()
 91     else:
 92         try:
 93             Handle = Tomcat(args.choices[0])
 94             if args.handle == 'log':
 95                 Handle.tomcat_log()
 96             elif args.handle == 'start':
 97                 Handle.start_tomcat()
 98             elif args.handle == 'stop':
 99                 Handle.stop_tomcat()
100             elif args.handle == 'restart':
101                 Handle.stop_tomcat()
102                 time.sleep(5)
103                 Handle.start_tomcat()
104             elif args.handle == 'status':
105                 if Handle.get_tomcat_pid() is not None:
106                     print "33[32m %s Is Running is PID:33[0m" % Handle.tomcat_exe + "33[31m %s 33[0m" % Handle.get_tomcat_pid()
107                 else:
108                     print "33[32m %s Not Running Or Not Exist 33[0m" % Handle.tomcat_exe
109 
110         except TypeError:
111             parser.print_help()
View Code

        

原文地址:https://www.cnblogs.com/edwardlogs/p/5055112.html