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
  • 相关阅读:
    yolo_to_onnx ValueError: need more tan 1 value to unpack
    yolo_to_onnx killed
    C++ 实现二维矩阵的加减乘等运算
    Leetcode 1013. Partition Array Into Three Parts With Equal Sum
    Leetcode 1014. Best Sightseeing Pair
    Leetcode 121. Best Time to Buy and Sell Stock
    Leetcode 219. Contains Duplicate II
    Leetcode 890. Find and Replace Pattern
    Leetcode 965. Univalued Binary Tree
    Leetcode 700. Search in a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/edwardlogs/p/5012782.html
Copyright © 2011-2022 走看看