歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Liunx下定時重啟tomcat實現代碼

1.restart.sh

#!/bin/sh

. /etc/profile

echo >> restartTomcat.log
echo >> restartTomcat.log
echo "=====================================================" >> restartTomcat.log
echo "=====[`date +'%y-%m-%d %H:%M:%S'`] start Task ======" >> restartTomcat.log
echo "=====================================================" >> restartTomcat.log

# 查看tomcat5是否有進程
pid=`ps aux | grep tomcat5 | grep -v grep | grep -v retomcat | awk '{print $2}'`

echo "AA: the process id=$pid" >> restartTomcat.log

if [ -n "$pid" ]
then
. /usr/java/retomcat.sh
else
echo "AA: [`date +'%y-%m-%d %H:%M:%S'`] [WARN] tomcat not start ,startup tomcat" >> restartTomcat.log
/usr/java/tomcat5/bin/startup.sh >> restartTomcat.log
fi

2.retomcat.sh
#!/bin/sh

echo "AA: [`date +'%y-%m-%d %H:%M:%S'`]tomcat is start ,will shutdown tomcat " >>restartTomcat.log
# 關閉tomcat5
/usr/java/tomcat5/bin/shutdown.sh >> restartTomcat.log
sleep 10s
pid=`ps aux | grep tomcat5 | grep -v grep | grep -v retomcat | awk '{print $2}'`
#查看是否關閉
while [ -n "$pid" ]; do

echo "AA: [`date +'%y-%m-%d %H:%M:%S'`] fail shutdown tomcat id = $pid " >> restartTomcat.log
echo "AA: [`date +'%y-%m-%d %H:%M:%S'`] kill tomcat id= $pid " >> restartTomcat.log
kill -9 $pid
sleep 1m
pid=`ps aux | grep tomcat5 | grep -v grep | grep -v retomcat | awk '{print $2}'`
done
#關閉成功,重啟
echo "AA: [`date +'%y-%m-%d %H:%M:%S'`] success shutdown tomcat " >> restartTomcat.log
sleep 3m
echo "AA: [`date +'%y-%m-%d %H:%M:%S'`]start startup tomcat " >> restartTomcat.log
/usr/java/tomcat5/bin/startup.sh >> restartTomcat.log


3.在添加定時任務

#crontab -e

0 5 * * * /usr/java/bak/restartTomcat.sh
表示在5點執行定時任務

4.賦於執行的權限

#chmod +x /usr/java/restart.sh

#chmod +x /usr/java/retomcat.sh

 

Copyright © Linux教程網 All Rights Reserved