[英]Bash Script: Apache Server is running or not
我正在嘗試使用bash腳本實現以下步驟:
1)檢查Apache Server
的狀態。
2)如果啟動並正在運行,則什么也不做。 如果不是,請轉到步驟3。
3)如果服務器未運行,請先發送失敗電子郵件並重新啟動服務器
4)重新啟動后,再次檢查狀態,並發送確認電子郵件
這是我的代碼:
#checking if Apache is running or not
ps auxw | grep apache2 | grep -v grep > /dev/null
if [ $? != 0 ]
then
mailx -s "Apache web server is down, Trying auto-restart" -$
# web server down, restart the server
sudo /etc/init.d/apache2 restart > /dev/null
sleep 10
#checking if apache restarted or not -- This is not working
ps auxw | grep apache2 | grep -v grep > /dev/null
if [ $? = 0 ]
then
mailx -s "Apache restarted succesfully" -r "$SENDEREMAIL" "$NOTIFYEMAIL" < /$
else
mailx -s "Restart Failed, try restarting manually" -r "$SENDEREMAIL" "$NOTIFYEMAIL" <$
fi
fi
該代碼在步驟3之前可以正常工作,而在步驟4上失敗/不起作用,即腳本在重新啟動並發送確認電子郵件后無法檢查服務器的狀態。 有人可以讓我知道我要去哪里了。
嘗試這個:
#checking if Apache is running or not
if ! pidof apache2 > /dev/null
then
mailx -s "Apache web server is down, Trying auto-restart"
# web server down, restart the server
sudo /etc/init.d/apache2 restart > /dev/null
sleep 10
#checking if apache restarted or not
if pidof apache2 > /dev/null
then
message="Apache restarted successfully"
else
message="Restart Failed, try restarting manually"
fi
mailx -s "$message" -r "$SENDEREMAIL" "$NOTIFYEMAIL"
fi
注意:每個mailx行的末尾都有-$
, < /$
或<$
-這些看起來像是錯別字,並已被刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.