Linux服务器中tomcat设置定时重启任务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MaBanSheng/article/details/78613950

一、实现思路
编写shell脚本,加入定时任务。

二、实现目的
tomcat定时重启防止项目卡死。

三、编写shell脚本(来源网络)

#!/bin/sh
export JAVA_HOME=**/usr/java/jdk1.7.0_80**
pid=`ps -auxww|grep tomcat|grep -v grep|awk '{print $2}'`
echo $pid
if [ -n "$pid" ];
then
{
echo "==========tomcat is start============"
'**/home/svc/tomcat/bin/shutdown.sh**'
sleep 5
'**/home/svc/tomcat/bin/shutdown.sh**'
sleep 15
pid=`ps -auxww|grep tomcat|grep -v grep|awk '{print $2}'`
if [ -n "$pid" ];
then
{
echo "======to kill the tomcat pid $pid========"
kill $pid
sleep 3
}
fi
echo "==========start tomcat============"
'**/home/svc/tomcat/bin/startup.sh**'
}
else
{
echo "==========tomcat is stop=====start======="
'**/home/svc/tomcat/bin/startup.sh**'
}
fi

上述shell脚本,复制、粘贴,将地址部分改为自己项目实际中jdk和tomcat的地址。

四、提高权限,使脚本可以执行
执行chmod +x filename

五、编辑添加定时
执行crontab -e 编辑定时时间

时间格式:
分< >时< >日< >月< >星期< >要运行的命令 ,其中< >表示空格。
例如:
1 04 * * * /home/svc/svcreboot.sh 表示每天凌晨4点执行

执行crontab -l 可以查询到定时任务

六、启动定时服务
service crond stop
service crond start

总结:当然这时一个简单的实现流程,想深入学习shell脚本的可以自行研究,通过这简单几4步,能帮助大家解决问题就好。

猜你喜欢

转载自blog.csdn.net/MaBanSheng/article/details/78613950