最近我在學(xué)生價買的低配服務(wù)器上部署了一個很吃內(nèi)存的網(wǎng)頁,導(dǎo)致 tomcat 內(nèi)存經(jīng)常溢出而崩潰。
于是我上網(wǎng)找了一些教程編寫了一個簡單的每天定時啟動 tomcat 的腳本,特此記錄一下
我的環(huán)境是 centos 7
1、 在某個目錄新建一個 .sh 腳本文件
2、 在 tomcatStart.sh 文件里面寫入一下代碼
#!/bin/bash
/etc/profile
tomcatPath="/usr/local/tomcat9"
binPath="$tomcatPath/bin"
echo "[info][$(date)]正在監(jiān)控tomcat,路徑:$tomcatPath"
pid=`ps -ef | grep tomcat | grep -w $tomcatPath | grep -v 'grep' | awk '{print $2}'`
if [-n "pid"]; then
echo "[info][$(date)]tomcat進(jìn)程為:$pid"
echo "[info][$(date)]tomcat已經(jīng)啟動,準(zhǔn)備使用shutdown命令關(guān)閉"
$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關(guān)閉失敗,準(zhǔn)備kill進(jìn)程"
kill -9 $pid
echo "[info][$(date)]kill進(jìn)程完畢"
sleep 1
else
echo "[info][$(date)]使用shutdown關(guān)閉成功"
fi
else
echo "[info][$(date)]tomcat未啟動"
fi
echo "[info][$(date)]準(zhǔn)備啟動tomcat"
$binPath"/startup.sh"
3、 修改 tomcatStart.sh 的權(quán)限
sudo chmod 777 tomcatStart.sh
4、 添加腳本到 crontab 定時任務(wù)
crontab -e
// 第一個是 tomcatStart.sh 的路徑, 第二個是將日志輸出到某個文件中
00 03 * * * /usr/local/tomcat9/bin/tomcatStart.sh >> /home/zhang/tomcatLog.txt
5、 重啟一下 crontab 以生效
除了這種手動重啟的方法,還有優(yōu)化 tomcat 的方法,這個等有時間再去探索了
到此這篇關(guān)于編寫shell腳本實(shí)現(xiàn)tomcat定時重啟的方法的文章就介紹到這了,更多相關(guān)shell tomcat定時重啟內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Shell定時刪除指定時間之前的文件
- 使用Linux shell腳本實(shí)現(xiàn)FTP定時執(zhí)行批量下載指定文件
- Linux shell實(shí)現(xiàn)每天定時備份mysql數(shù)據(jù)庫
- Shell腳本定時備份清除運(yùn)行系統(tǒng)日志的代碼
- Linux Crontab Shell腳本實(shí)現(xiàn)秒級定時任務(wù)的方法
- CentOS下mysql定時備份Shell腳本分享
- shell腳本實(shí)現(xiàn)mysql定時備份、刪除、恢復(fù)功能
- PowerShell小技巧之定時記錄操作系統(tǒng)行為