Linux下简单的tomcat管理脚本

如今使用Linux做为项目运行环境的已经愈来愈多,Linux下tomcat启动默认是看不到输出信息的,如平常开发中能够在eclipse中看到的输出信息都被输出到logs/catalina.out.须要查看的话能够使用python

tail -f logs/catalina.out

笔者常常须要发布项目新版本,须要不断操做tomcat启动和关闭。因而写了如下tomcat.py的Python脚本:apache

# encoding:utf-8

import os, sys

start_operation = ['startup', '-startup', '--startup', 'start', '-start', '--start']
stop_operation  = ['shutdown', '-shutdown', '--shutdown', 'stop', '-stop', '--stop']
show_operation  = ['show', 'log']

def show_help():
	print 'usage:  python tomcat.py start|stop|show'
	print '\n\n'
	print 'startup operation : '
	print start_operation
	print '----------------------------'
	print 'stop operation    : '
	print stop_operation
	print '----------------------------'
	print 'show operation    :'
	print show_operation
	print '----------------------------'

if __name__=="__main__":
	operation = ''
	try:
		operation = sys.argv[1]
	except:
		show_help()
		sys.exit(0)
	if operation in start_operation:
		# start tomcat
		os.system('/opt/apache-tomcat-8.0.32/bin/shutdown.sh')
		os.system('/opt/apache-tomcat-8.0.32/bin/startup.sh')
		os.system('tail -f /opt/apache-tomcat-8.0.32/logs/catalina.out')
	elif operation in stop_operation:
		# stop tomcat
		os.system('/opt/apache-tomcat-8.0.32/bin/shutdown.sh')
		os.system('tail -f /opt/apache-tomcat-8.0.32/logs/catalina.out')
	elif operation in show_operation:
		os.system('tail -f /opt/apache-tomcat-8.0.32/logs/catalina.out')
	else:
		show_help()
		sys.exit(0)

注释一下:tomcat

  • show_help()是在没有输入指令的时候进行输出显示用法的函数。
  • 一共有三种操做,start,stop,show.为了知足不一样的习惯,每种操做有各类开关习惯。(感受这里确实用得有点麻烦,顺便给出一种读取开关指令并控制操做的方法。)
  • os.system()就是经过python调用操做系统中的指令。
相关文章
相关标签/搜索