编写脚本
tomcatMonitor.sh
#!/bin/sh. /etc/profile
. ~/.bash_profile#首先用ps -ef | grep tomcat 获得了tomcat进程信息,这样出来的结果中会包含grep本身,
#因此通过 | grep -v grep 来排除grep本身,然后通过 awk '{print $2}'来打印出要找的进程id号;
pid=$(ps -ef|grep apache-tomcat-8.5.47 |grep -v grep | awk '{print $2}')
#echo $pidif [ -n "$pid" ];then echo "tomcat进程ID为:$pid. 运行正常"
else echo "进程不存在! Tomcat自动重启" cd /usr/local/apache-tomcat-8.5.47/bin/ && ./startup.sh
fi
为脚本添加可执行权限
chmod u+x tomcatMonitor.sh
配置定时任务
*/30 * * * * /usr/local/tomcatMonitor.sh
重启定时任务:systemctl start crond