1.创建shell脚本
vim /etc/init.d/tomcat.sh
#!/bin/bash . /etc/profile export JAVA_HOME=/usr/local/jdk export JRE_HOME=$JAVA_HOME/jre tomcatPath="/usr/local/tomcat" binPath="$tomcatPath/bin" echo "[info][$(date +‘%F %H:%M:%S‘)]正在监控tomcat,路径:$tomcatPath" pid=`ps -ef|grep java | grep catalina | awk ‘{print $2}‘` if [ -n "$pid" ]; then echo "[info][$(date +‘%F %H:%M:%S‘)]正在运行的tomcat进程为:$pid" echo "[info][$(date +‘%F %H:%M:%S‘)]tomcat已经启动,准备使用shutdown命令关闭..." $binPath"/shutdown.sh" sleep 2 pid=`ps -ef|grep java | grep catalina | awk ‘{print $2}‘` if [ -n "$pid" ]; then echo "[info][$(date +‘%F %H:%M:%S‘)]使用shutdown命令关闭失败,准备kill进程..." kill -9 $pid echo "[info][$(date +‘%F %H:%M:%S‘)]kill进程完毕!" sleep 1 else echo "[info][$(date +‘%F %H:%M:%S‘)]使用shutdown命令关闭成功!" fi else echo "[info][$(date +‘%F %H:%M:%S‘)]tomcat未启动!" fi echo "[info][$(date +‘%F %H:%M:%S‘)]准备启动tomcat..." $binPath"/startup.sh"
2.修改权限
chmod -R 777 /etc/init.d/tomcat.sh
3.创建定时任务
crontab -e
4.查看定时任务
5.重启crond服务
service crond restart
6.查看脚本日志
tail -f /var/spool/mail/root -n 20