crontab + 檢測錯誤 + 自動重啟

顯示ˋ執行相關的 log 紀錄

grep CRON /var/log/syslog* | grep -E "check-httpd.sh"

添加到自動執行程序

crontab -e
* * * * * sh /home/user/check-httpd.sh

crontab -l

第一種

#!/bin/bash
DATE=$(date)
/usr/bin/curl --max-time 5 -s --head  --request GET http://127.0.0.1/ | if ! grep "200 OK"; then
/opt/bitnami/ctlscript.sh restart apache
echo "$DATE - NOT OKAY, apache restarted" >> /home/user/custom-restarts.log
fi

看一下重重啟了幾次

tail custom-restarts.log 

第二種

curl --max-time 5 http://127.0.0.1/ || /opt/bitnami/ctlscript.sh restart apache

第三種

#!/bin/bash


# Check httpd header
checkhttpd=`/usr/bin/curl -s --head --request GET http://127.0.0.1/  | grep HTTP | /usr/bin/wc -l`

echo = $checkhttpd

if [ $checkhttpd != 1 ]; then
echo '!=1'
echo 'restart httpd'
/opt/bitnami/ctlscript.sh restart apache
fi
Written on October 10, 2020