Tomcat Script (Python)

Source: Internet
Author: User

With Python in mind, write some small scripts in Python and share them with you at the same time.

#!/usr/bin/env python#_*_coding:utf-8_*_#Author: ' Lonny '#dateTime: ' 15/11/26 '#motto: ' Good memory as bad written 'ImportsubprocessImportTime , sysImportPlatformImportPsutilclassTomcat (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=counntdefSystem_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/1024Can_use= Psutil.virtual_memory (). available/1024IPAddress= str (Psutil.net_if_addrs () ['eth0'][0]) processors= Psutil.cpu_count (logical=False)Print "||||||||||"+"\033[32m Native IP address--->eth0:"+"\033[31m%s\033[0m"% Ipaddress.split (',') [1]        Print "||||||||||"+"\033[32m System Information: \033[0m"+"\033[31m%s%s-%s%s\033[0m"%(System_type, system_version[0], system_version[1], system_bit[0])Print "||||||||||"+"\033[32m Native boot time: \033[0m"+"\033[31m%s\033[0m"%Boot_startPrint "||||||||||"+"\033[32m Native Total Memory: \033[0m"+"\033[31m%skb\033[0m"% host_memory +"\033[31m---->%SMB \033[0m"%(Host_memory/1024)        Print "||||||||||"+"\033[32m memory available for use with this machine: \033[0m"+"\033[31m%skb\033[0m"% can_use +"\033[31m---->%SMB \033[0m"%(Can_use/1024)        Print "||||||||||"+"\033[32m Native CPU composite: \033[0m"+"\033[31m%s \033[0m"%Processorsdefget_tomcat_pid (self): Commandcheckoutpid="PS aux | grep java | grep-v grep | grep ' Jenkins_tomcat ' | awk ' {print $} '"P= subprocess. Popen (Commandcheckoutpid, stdin=subprocess. PIPE, stdout=subprocess. PIPE, stderr=subprocess. PIPE, shell=True) stdout, stderr=p.communicate ()#print "\033[32m Jenkins is starting \033[0m" + "\033[31mpid:%s\033[0m"% stdout        returnstdoutdefStart_tomcat (self):ifLen (Self.get_tomcat_pid (). Strip ())! =0:Print "\033[32m Tomcat is Started \033[0m"        Else:            #Start TomcatCommand_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 ()PrintstdoutdefStop_tomcat (self): Wait_sleep=0ifLen (Self.get_tomcat_pid (). Strip ()) = =0:Print "\033[32m Tomcat is not Running \033[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 "\nwaiting for processes to exit"Wait_sleep+ = 1Time.sleep (1)                ifWait_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 "\033[32m Stop Tomcat is sucessful \033[0m"                     Break    defuseage (self): Scrip_name="jenkins.py"        Print "\033[32m script use \033[0m"+"./"+ Scrip_name +"\033[31m start|stop|restart|status\033[0m"        Print "or"        Print "\033[32m Script Use \033[0m"+"python <path>/"+ Scrip_name +"\033[31m start|stop|restart|status\033[0m"    defTomcat_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:             whileReturnCode isNone:line=p.stdout.readline () ReturnCode=p.poll () Line=Line.strip ()Print LinePrintReturnCodeexceptKeyboardinterrupt:Print 'ctrl+d or Z'if __name__=='__main__': Handle= Tomcat ("/software/jenkins_tomcat","/software/jenkins_tomcat/logs")    ifLen (SYS.ARGV) < 2: Handle.useage () sys.exit ()elifSYS.ARGV[1] = ='Start': Handle.start_tomcat ()elifSYS.ARGV[1] = ='Stop': Handle.stop_tomcat ()elifSYS.ARGV[1] = ='Restart': Handle.stop_tomcat () time.sleep (5) Handle.start_tomcat ()elifSYS.ARGV[1] = ='Info': Handle.system_info ()elifSYS.ARGV[1] = ='Log': Handle.tomcat_log ()elifSYS.ARGV[1] = ='Status':        ifLen (Handle.get_tomcat_pid (). Strip ())! =0:Print "\033[32m Tomcat is Running is pid:\033[0m"+"\033[31m%s \033[0m"%handle.get_tomcat_pid (). Strip ()Else:            Print "\033[32m Tomat not Running \033[0m"    Else: Handle.useage ()
View Code

Tomcat Script (Python)

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.