1.健康檢查腳本:health.sh
#!/bin/bash
datestr=`date "+%Y-%m-%d %H:%M:%S"`
echo "$datestr : check start"
curl -s localhost:8983/solr > /dev/null
[ $? -eq 0 ] && echo "$datestr : server is running"
[ $? -ne 0 ] && echo "$datestr : server is not running, restart" && /var/www/file/images2/solr/solr-8.5.0/bin/solr restart -force -m 4g
解釋,如果curl運行不成功,則運行&&後的腳本
2.crontab,每分鐘檢查一次,重啓服務器後crontab也會自動重啓,仍然每分鐘檢查
[root@solr]# crontab -e
## 檢查solr服務,自動重啓
*/1 * * * * nohup sh /var/www/file/images2/solr/health.sh 1>>"/var/www/file/images2/solr/logs/health.log" 2>&1 </dev/null &
解釋:編輯crontab,每分鐘執行健康檢查腳本,疊加輸出日誌到health.log