tomcat启动与关闭脚本

需求:
apache-tomcat-7.0.37

python2.*

 

#!/usr/bin/evn python
# _*_ coding:utf-8 _*_
import commands
import time

###############REDMAIN################

#此脚本只能用于tomcat服务restart。如果有新的tomcat需要修改以下三点
#1、把tomcat源文件路径写入classpath=""中
#2、把tomcat缓存路径写到tomcatcache=""
#3、修改当前启动的tomcat端口在脚本最下方有tomcat_port='8000'参数,这里默认是8000

#切记:以上两点必须绝对路径,一个脚本只能对应一个tomcat

#####################################


classpath = "/opt/apache-tomcat-7.0.37"
tomcatcache = "/opt/apache-tomcat-7.0.37/work"
file_time = time.strftime('%Y%m%d_%H:%M:%S')

def tomcat_start():
  print("正在启动.....")
  time.sleep(5)
  start_status,start_tomcat = commands.getstatusoutput("/bin/bash %s/bin/startup.sh" % classpath)
  print(start_status,start_tomcat)

def tomcat_cache_mv():
  mv_command = ("mv %s/Catalina %s/Catalina_%s" % (tomcatcache,tomcatcache,file_time))
  commands.getstatusoutput(mv_command)
  print("缓存移动中.....")
  time.sleep(5)
  file_status,file_none = commands.getstatusoutput("ls %s/*_%s" % (tomcatcache,file_time))
  print(file_none)
  if file_none == "":
    print("缓存已移动,%s自动启动中!" % classpath)+time.strftime(' == %Y%m%d %H:%M:%S')


def tomcat_stop():
  print("%s关闭中....!" % classpath)+time.strftime(' == %Y%m%d %H:%M:%S')
  commands.getstatusoutput("kill -9 %s" % pids)
  time.sleep(5)

if __name__ == "__main__":
  tomcat_port = "8000"
  status,pids = commands.getstatusoutput("ps aux | grep %s | grep -v 'grep' | awk '{print $2}'"% classpath)
  tomcat_post_status,tomcat_post = commands.getstatusoutput("netstat -luntp| grep %s" % tomcat_port)
  if pids == "" and tomcat_post =="":
    print("%s未启用!" % classpath)
    tomcat_cache_mv()
    tomcat_start()
  else:
    print("%s正在动行,即将关闭tomcat!" % classpath)
    tomcat_stop()
    tomcat_cache_mv()
    tomcat_start()

posted @ 2017-04-19 19:07  Tale_G  阅读(1463)  评论(0编辑  收藏  举报