Python入门实例之三:tomcat运维脚本(含启动、关闭、重启、查看日志功能)

使用手册:

y:终止进程并重新启动;k:仅终止进程,s:仅启动;:查看日志;输入进程号:终止进程并重启

注意事件:

server_name为基于/usr/local的目录名,设置后即可使用。

# encoding:utf8
import commands
import os

server_name = 'tomcat/8080'

os.system('clear; ps -ef |grep -v \'grep\' |grep -v \'tail -f\' |grep %s --color=auto' % server_name)

return_code, output = commands.getstatusoutput('ps -ef |grep -v \'grep\' |grep -v \'tail -f\' |grep %s |awk \'{print $2}\'' % server_name)
cmd = raw_input('[%s]Your choice: ' % output)

if cmd.lower() == 'y':
   os.system('/usr/local/%s/bin/startup.sh && kill -9 %s' % (server_name, output))
   os.system('/usr/local/%s/bin/startup.sh' % server_name)
   os.system('tail -f /usr/local/%s/logs/catalina.out' % server_name)
elif cmd.lower() == 's':
   os.system('/usr/local/%s/bin/startup.sh' % server_name)
   os.system('tail -f /usr/local/%s/logs/catalina.out' % server_name)
elif cmd.lower() == 'k':
   os.system('/usr/local/%s/bin/startup.sh && kill -9 %s' % (server_name, output))
   print 'graceful exit ..'
elif cmd.isdigit() and int(cmd) > 1000:
   os.system('/usr/local/%s/bin/startup.sh && kill -9 %s && /usr/local/%s/bin/startup.sh && tail -f /usr/local/%s/logs/catalina.out' % (server_name, output, server_name, server_name))
elif cmd.isspace():
   os.system('tail -f /usr/local/%s/logs/catalina.out' % server_name)
else:
   print 'graceful exit ..'

你可能感兴趣的:(Python)