1.监控脚本
#!/usr/local/python3/bin/python3.6# -*-coding:utf-8-*-importsubprocessimportdatetimeres=subprocess.Popen('ps -ef | grep tomcat',stdout=subprocess.PIPE,shell=True) tomcats=res.stdout.readlines() counts=len(tomcats) ifcounts<4: dt=datetime.datetime.now() fp=open('/root/lin/log/tomcat.log','a') fp.write('tomcat6 stop at %s\n'%dt.strftime('%Y-%m-%d %H:%M:%S')) fp.close() subprocess.Popen('/root/lin/run/apache-tomcat-9.0.19/bin/startup.sh',shell=True)
2.定时任务
vim /etc/crontab
#加入如下信息 每十分钟运行该脚本一次
*/10 * * * * root python /root/autorestart-tomcat.py