zoukankan      html  css  js  c++  java
  • Tomcat Script(python)

    由于刚接触 Python,所以使用Python 书写一些小的脚本,进行备忘同时分享给大家

    #!/usr/bin/env python
    # _*_coding:utf-8_*_
    #  author:  'lonny'
    # dateTime:  '15/11/26'
    #   motto:  'Good memory as bad written'
    
    
    import subprocess
    import time, sys
    import platform
    import psutil
    
    
    class Tomcat(object):
        def __init__(self, Tomcat_Home, Tomcat_Log_Home, counnt=10):
            self.Tomcat_Home = Tomcat_Home
            self.Tomcat_Log_Home = Tomcat_Log_Home
            self.counnt = counnt
    
        def system_info(self):
            system_type = platform.system()
            system_version = platform.linux_distribution()
            system_bit = platform.architecture()
            Boot_Start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(psutil.boot_time()))
            Host_Memory = psutil.virtual_memory().total / 1024
            Can_Use = psutil.virtual_memory().available / 1024
            ipaddress = str(psutil.net_if_addrs()['eth0'][0])
            processors = psutil.cpu_count(logical=False)
    
            print "||||||||||" + "33[32m本机IP地址--->Eth0:" + "33[31m %s33[0m" % ipaddress.split(',')[1]
            print "||||||||||" + "33[32m系统信息:33[0m" + "33[31m%s %s-%s %s33[0m" % (
                system_type, system_version[0], system_version[1], system_bit[0])
            print "||||||||||" + "33[32m本机启动时间:33[0m" + "33[31m%s33[0m" % Boot_Start
            print "||||||||||" + "33[32m本机总内存:33[0m" + "33[31m %sKB33[0m" % Host_Memory + "33[31m ---->%sMB 33[0m" % (
                Host_Memory / 1024)
            print "||||||||||" + "33[32m本机可使用内存:33[0m" + "33[31m%sKB33[0m" % Can_Use + "33[31m ---->%sMB 33[0m" % (
                Can_Use / 1024)
            print "||||||||||" + "33[32m本机cpu合数:33[0m" + "33[31m %s 33[0m" % processors
    
        def get_tomcat_pid(self):
            commandcheckoutpid = "ps aux | grep java | grep -v grep | grep 'jenkins_tomcat' | awk '{print $2}'"
            p = subprocess.Popen(commandcheckoutpid, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE, shell=True)
            stdout, stderr = p.communicate()
            # print "33[32m Jenkins Is Starting 33[0m" + "33[31mPID:%s33[0m" % stdout
            return stdout
    
        def start_tomcat(self):
            if len(self.get_tomcat_pid().strip()) != 0:
                print "33[32m Tomcat Is Started 33[0m"
            else:
                # Start Tomcat
                command_start_tomcat = "%s/bin/startup.sh" % self.Tomcat_Home
                p = subprocess.Popen(command_start_tomcat, stdin=subprocess.PIPE, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
                stdout, stderr = p.communicate()
                print stdout
    
        def stop_tomcat(self):
            wait_sleep = 0
            if len(self.get_tomcat_pid().strip()) == 0:
                print "33[32m Tomcat is Not Running 33[0m"
            else:
                command_stop_tomcat = "%s/bin/shutdown.sh" % self.Tomcat_Home
                p = subprocess.Popen(command_stop_tomcat, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE, shell=True)
                stdout, stderr = p.communicate()
                while (len(self.get_tomcat_pid().strip()) != 0):
                    print "
    waiting for processes to exit"
                    wait_sleep += 1
                    time.sleep(1)
                    if wait_sleep == self.counnt:
                        command_Kill_tomcat = "kill -9 %s" % int(self.get_tomcat_pid().strip())
                        p = subprocess.Popen(command_Kill_tomcat, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                             stderr=subprocess.PIPE, shell=True)
                        stdout, stderr = p.communicate()
                        print "33[32m Stop Tomcat is sucessful 33[0m"
                        break
    
        def useage(self):
            scrip_name = "jenkins.py"
            print "33[32m script use 33[0m" + "./" + scrip_name + "33[31m start|stop|restart|status33[0m"
            print "or"
            print "33[32m Script use 33[0m" + "python <path>/" + scrip_name + "33[31m start|stop|restart|status33[0m"
    
        def tomcat_log(self):
            command_tomcat_log = "tail -f %s/catalina.out " % self.Tomcat_Log_Home
            p = subprocess.Popen(command_tomcat_log, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
            returncode = p.poll()
            try:
                while returncode is None:
                    line = p.stdout.readline()
                    returncode = p.poll()
                    line = line.strip()
                    print line
                print returncode
            except KeyboardInterrupt:
                print 'ctrl+d or z'
    
    
    if __name__ == '__main__':
        Handle = Tomcat("/software/jenkins_tomcat", "/software/jenkins_tomcat/logs")
        if len(sys.argv) < 2:
            Handle.useage()
            sys.exit()
        elif sys.argv[1] == 'start':
            Handle.start_tomcat()
        elif sys.argv[1] == 'stop':
            Handle.stop_tomcat()
        elif sys.argv[1] == 'restart':
            Handle.stop_tomcat()
            time.sleep(5)
            Handle.start_tomcat()
        elif sys.argv[1] == 'info':
            Handle.system_info()
        elif sys.argv[1] == 'log':
            Handle.tomcat_log()
        elif sys.argv[1] == 'status':
            if len(Handle.get_tomcat_pid().strip()) != 0:
                print "33[32m Tomcat Is Running is PID:33[0m" + "33[31m %s 33[0m" % Handle.get_tomcat_pid().strip()
            else:
                print "33[32m Tomat Not Running 33[0m"
        else:
            Handle.useage()
    View Code
  • 相关阅读:
    k8s环境搭建--基于minik8s方法
    k8s环境搭建--基于kubeadm方法
    Ubuntu 搭建简单的git server
    国密算法--Openssl 实现国密算法(加密和解密)
    国密算法--Openssl 实现国密算法(基础介绍和产生秘钥对)
    Hyperledger Fabric 1.1 -- Policy 构成
    Pycharm 2018.2.1-2018.1
    6-5 链式表操作集
    6-4 链式表的按序号查找
    6-3 求链式表的表长
  • 原文地址:https://www.cnblogs.com/edwardlogs/p/5012782.html
Copyright © 2011-2022 走看看