通过检测tomcat端口是否有开启来启动tomcat服务的一个守护脚本 需要安装daemonocle包 安装方法: pip install daemonocle 1.#!/usr/bin/env python 2.# coding:utf-8 3.# Author: Edward -- <edgeman_03@163.com> 4.# Purpose: 监控tomcat是否正常运行 5.# Created: 2014-6-16 6. 7.import sys 8.import socket 9.import os 10.import time 11.import logging 12.import daemonocle 13. 14.HOST = '127.0.0.1' 15.PORTS = [8080, 8081, 8082] 16. 17.def cb_shutdown(message, code): 18. logging.info('Daemon is stopping') 19. logging.debug(message) 20. 21.def check_service(address, port): 22. s = socket.socket() 23. print "Attempting to connect to %s on port %s" % (address, port) 24. try: 25. s.connect((address, port)) 26. print "Connect to %s on port %s" % (address, port) 27. return True 28. except socket.error, e: 29. print "Connect to %s on port %s failed: %s" % (address, port, e) 30. return False 31. 32.def main(): 33. logging.basicConfig( 34. filename='/var/log/monitor_tomcat.log', 35. level=logging.DEBUG, format='%(asctime)s [%(levelname)s] %(message)s', 36. ) 37. logging.info('Monitor Tomcat Daemon is starting') 38. while True: 39. if check_service(HOST, PORTS[0]) == False: 40. time.sleep(2) 41. os.system("/etc/init.d/tomcat start") 42. if check_service(HOST, PORTS[1]) == False: 43. time.sleep(2) 44. os.system("/etc/init.d/tomcat2 start") 45. if check_service(HOST, PORTS[2]) == False: 46. time.sleep(2) 47. os.system("/etc/init.d/tomcat3 start") 48. 49.if __name__=='__main__': 50. daemon = daemonocle.Daemon( 51. worker= main, 52. pidfile= '/var/run/monitor_tomcat.pid', 53. shutdown_callback= cb_shutdown, 54. ) 55. daemon.do_action(sys.argv[1]) 脚本使用方法: 启动脚本 ./monitor_tomcat.py start 查看脚本运行状态 ./monitor_tomcat.py status 停止脚本 ./monitor_tomcat.py stop (责任编辑:IT) |