This post was last edited by envfull at, 2011-9-21
#! /Usr/bin/ENV Python #-*-Coding = UTF-8 -*- """ 1. automatically start Tomcat; 2. automatically disable Tomcat; 3. automatically repeat Tomcat; """ Import OS Import sys Import time Import glob Import telnetlib Host = "127.0.0.1" Port = "80" Tomcat = "/var/Apache-Tomcat-6.0.18/" # Tomcat path # Clear all Tomcat logs Def clearlog (): OS. System ("cat" + "/dev/null" + ">" + Tomcat + "logs/Catalina. Out ") Map (OS. Remove, glob. glob (Tomcat + "logs/*. log ")) Map (OS. Remove, glob. glob (Tomcat + "logs/*. txt ")) # Disable the Tomcat server ---------------------------------------------------------- Def Shutdown (): Clearlog () Print "Tomcat is shutting down. Please wait for 13 seconds... " OS. System (Tomcat + "bin/shutdown. Sh ") OS. System ("Kill-9 'ps-Ef | grep Java | awk '{print $2 }''") Time. Sleep (13) Try: Tel = telnetlib. telnet (host, Port) Print "failed to close tomcat. Please close it manually" SYS. Exit () Except t: Print "Tomcat disabled successfully"
# Start the Tomcat server ---------------------------------------------------------- Def startup (): Clearlog () Print "Tomcat is starting. After the advertisement, return immediately... " OS. System (Tomcat + "bin/startup. Sh ") Time. Sleep (3) For k in range (38, 0,-1 ): Print K Time. Sleep (1) OS. System ("clear ") # Determine whether startup is successful Rizhi = open (Tomcat + "logs/Catalina. Out", 'R ') Try: Telnet = telnetlib. telnet (host, Port) Xinxi = rizhi. Read () Xinxi. Index ("server startup in ") Print "Let you wait and reveal the answer in five seconds" Time. Sleep (6) Print "Tomcat started successfully. You are lucky this time! " Rizhi. Close () Except t: Print "failed to start Tomcat, as I expected! " Rizhi. Close ()
# Restart the Tomcat server ---------------------------------------------------------- Def Chongqi (): OS. System ("service MySQL restart ") Print "Haha, the database is restarted successfully. Restart Tomcat 6 seconds later! " Time. Sleep (6)
# Disable Tomcat Shutdown () # Start Tomcat Startup () # View or search Tomcat logs ----------------------------------------------------- Def viewlog (): If sousuo = "": OS. System ("cat" + Tomcat + "logs/Catalina. Out ") Else: OS. System ("grep-n" + sousuo + "" + Tomcat + "logs/Catalina. Out ")
#----------------------------------------------------------- Def main (): Print "| -------------------------------------------------------------------------- | \ n \ Welcome to rmao! \ N \ Rmao st # Start the Tomcat server \ n \ Rmao sh # disable Tomcat server \ n \ Rmao R # restart Tomcat server \ n \ Rmao L # view Tomcat logs \ n \ Rmao C # Clear Tomcat logs \ n \ \ N \ Rmao l startup # search for the startup keyword \ n \ in Catalina. Out \ \ N \ | -------------------------------------------------------------------------- | \ N" OS. System ("netstat-tnlp ") Print "-----------------------------------------------------------------------" OS. System ("DF-h ") Print "-----------------------------------------------------------------------" If _ name _ = "_ main __": If Len (SYS. argv) <2: Main () SYS. Exit () Else: Pass
Tihuan = SYS. argv [1] Sousuo = "" If Len (SYS. argv) = 3: Sousuo = SYS. argv [2] If tihuan = "R ": Chongqi () Elif tihuan = "St ": Startup () Elif tihuan = "sh ": Shutdown () Elif tihuan = "L ": Viewlog () Elif tihuan = "C ": Clearlog () Else: Main () SYS. Exit () |