[英]bash script: to check if Apache server is up and running
我是 bash 腳本的新手,並試圖弄清楚為什么下面的腳本輸出Apache server is not running
而它運行正常。
ps cax | grep httpd
if [ $? -eq 0 ]; then
echo "Process is running."
else
echo "Process is not running."
fi
我在Ubuntu 14.04.2 LTS
上運行它
另外,我如何更改腳本以測試安裝在另一台機器上的 apache 服務器。 請幫助
使用這個:
service apache2 status
或者這個:
service --status-all | grep apache2
這是一個 bash 腳本的工作示例,它檢查 apache 狀態,如果關閉則自動重新啟動,並通過 unicode 表情符號中的電報機器人發出警報。
#!/bin/bash
telegram=(xxxxx, yyyyyy)
if ! pidof apache2 > /dev/null
then
# web server down, restart the server
echo "Server down"
/etc/init.d/apache2 restart > /dev/null
sleep 10
#checking if apache restarted or not
if pidof apache2 > /dev/null
then
for i in "${telegram[@]}"
do
curl -s -X POST https://api.telegram.org/botxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyyyy/sendMessage -d chat_id="$i" -d text="`echo -e '\U0001F525'` Apache stoped on Molib Stage. Automatically restarted succesfully."
done
else
for i in "${telegram[@]}"
do
curl -s -X POST https://api.telegram.org/botxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyyyy/sendMessage -d chat_id="$i" -d text="`echo -e '\U0001F525'` Apache stoped on Molib Stage. Automatically restart failed. Please check manually."
done
fi
fi
而不是 httpd 嘗試 grep “ apache2 ”。 確保嘗試使用下一個命令檢查服務並確定 apache 網絡服務器的注冊名稱: service --status-all
## Plz run this script .. its working
------------------------------------------------
ps cax | grep httpd
if [ $? -eq 1 ]
then
echo "Process is running."
else if [ $? -eq 0 ]
echo "Process is not running."
fi
fi
----------------------------------------------
這項工作在舊的 Debian 中非常完美。 請記住使用 bash 而不是 sh 運行。
在 Centos 中替換為 httpd。
#!/bin/bash
if [ $(/etc/init.d/apache2 status | grep -v grep | grep 'Apache2 is running' | wc -l) > 0 ]
then
echo "Process is running."
else
echo "Process is not running."
fi
這是菜單驅動的一站式 shell 腳本,您可以在其中檢查防火牆、apache 或任何其他 Web 服務,只需選擇以下腳本中的選項即可啟動或停止服務
echo "welcome please select your options"
read choice
firewall=`sudo systemctl status firewalld`
apache=`sudo systemctl status apache2`
firewall1=`sudo systemctl stop firewalld`
apache1=`sudo systemctl stop apache2`
startrfirewall=`sudo systemctl start firewalld`
startapache=`sudo systemctl start apache2`
case $choice in
1) status of the firewall is $firewall
;;
2) status of apache is $apache
;;
3) echo stop firewall by $firewall1
;;
4) echo stop apache by $apache1
;;
5) echo start firewall by $startrfirewall
;;
6) echo start apache by $startapache
;;
*) echo exit
esac
試試看 - 在這里最簡單,最有教益,並且在 Ubuntu 20.04 上運行良好:
- 捕獲狀態輸出到 bash 變量
- “if”狀態包括子字符串(來自“Active:”語句) - 做你想做的工作
- "else" - 做你定義的另一項工作
#!/bin/bash
servstat=$(service apache2 status)
if [[ $servstat == *"active (running)"* ]]; then
echo "process is running"
else echo "process is not running"
fi
我根據以上將其組合在一起並制作了可以使用其他服務。
希望這可以幫助。
#!/bin/bash
# Must be running as root or via sudo permissions to be able to restart
# Put your process name restart command line in
PROCESS_NAME=httpd
if ! pidof $PROCESS_NAME > /dev/null
then
# web server down, restart the server
echo "Server $PROCESS_NAME down"
/usr/sbin/apachectl restart > /dev/null
echo "Tried restart of $PROCESS_NAME. Waiting 10 seconds to settle."
# wait ten
sleep 10
#checking if process restarted or not
if pidof $PROCESS_NAME > /dev/null
then
echo "$PROCESS_NAME was down but is now up."
else
echo "$PROCESS_NAME is still down. Please take some action."
fi
else
echo "Server $PROCESS_NAME up."
fi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.