Linux上編寫監控jar包重啟指令碼
阿新 • • 發佈:2022-05-18
一、centos7安裝crontab
# 安裝
yum -y install crontabs
# 檢視狀態
systemctl status crond
# 啟動
systemctl start crond
# 開機啟動
systemctl enable crond.service
二、編寫自動重啟指令碼
進入目錄:/home/haolb
vi publicsecurity_cron.sh
#!/bin/sh #公安三維--重啟命令 cd /home/haolb # 查詢程式佔用 project=`ps -ef|grep publicsecurity-jeecg-boot-module-system.jar|grep -v grep|wc -l` date=`date` server_name="oyz_publicsecurity-jeecg-boot-module-system" # $? -ne 0 不存在 # $? -eq 0存在 if [ ${project} -eq 0 ] then echo "時間:${date},${server_name}伺服器已掛,重啟中..." #停止服務 systemctl stop ${server_name} #時間阻斷:10s sleep 10s #啟動服務 systemctl start ${server_name} echo `date+%Y-%m-%d` `date +%H:%M:%S` $server_name >> /home/haolb/restartLogs/restart.log echo "時間:${date},重啟完成!" else echo "時間:${date},${server_name}服務【正常】" fi
三、如果指令碼在windows上編寫,則必須轉碼
shell指令碼報錯/bin/bash^M: bad interpreter: No such file or directory,通過查閱資料得知,shell指令碼格式必須是unix才行,但我這個指令碼是在windows上編寫完成傳到Linux伺服器上的,所以一執行就報錯: