Restart Tomcat script

Source: Internet
Author: User
Tags glob
[Python] restarting the Tomcat script is not well written. [copy the LINK]
   
  Direct elevator

1#

Posted on 16:07:01| View only the author | view in reverse order
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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.