這篇文章將為大家詳細講解有關利用shell腳本怎么定時重啟tomcat,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
1、 在某個目錄新建一個 .sh 腳本文件
vim tomcatStart.sh
2、 在 tomcatStart.sh 文件里面寫入一下代碼
#!/bin/bash
/etc/profile
tomcatPath="/usr/local/tomcat9"
binPath="$tomcatPath/bin"
echo "[info][$(date)]正在監控tomcat,路徑:$tomcatPath"
pid=`ps -ef | grep tomcat | grep -w $tomcatPath | grep -v 'grep' | awk '{print $2}'`
if [-n "pid"]; then
echo "[info][$(date)]tomcat進程為:$pid"
echo "[info][$(date)]tomcat已經啟動,準備使用shutdown命令關閉"
$binPath"/shutdown.sh"
sleep 2
pid=`ps -ef | grep tomcat | grep -w $tomcatPath | grep -v 'grep' | awk '{print $2}'`
if [-n "$pid"]; then
echo "[info][$(date)]使用shutdown關閉失敗,準備kill進程"
kill -9 $pid
echo "[info][$(date)]kill進程完畢"
sleep 1
else
echo "[info][$(date)]使用shutdown關閉成功"
fi
else
echo "[info][$(date)]tomcat未啟動"
fi
echo "[info][$(date)]準備啟動tomcat"
$binPath"/startup.sh"3、 修改 tomcatStart.sh 的權限
sudo chmod 777 tomcatStart.sh
4、 添加腳本到 crontab 定時任務
crontab -e // 第一個是 tomcatStart.sh 的路徑, 第二個是將日志輸出到某個文件中 00 03 * * * /usr/local/tomcat9/bin/tomcatStart.sh >> /home/zhang/tomcatLog.txt
5、 重啟一下 crontab 以生效
systemctl restart crond
關于利用shell腳本怎么定時重啟tomcat就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。