简体   繁体   English

Bash脚本:Apache服务器是否正在运行

[英]Bash Script: Apache Server is running or not

I'm trying to achieve the following steps using bash script: 我正在尝试使用bash脚本实现以下步骤:

1) Check the status of Apache Server . 1)检查Apache Server的状态。

2) If it is up and running, do nothing. 2)如果启动并正在运行,则什么也不做。 If it is not, then go to step 3. 如果不是,请转到步骤3。

3) If server is not running, then send a failure email first and restart the server 3)如果服务器未运行,请先发送失败电子邮件并重新启动服务器

4) After restarting, check the status again, and send a confirmation email 4)重新启动后,再次检查状态,并发送确认电子邮件

Here is my code: 这是我的代码:

#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

The code is working properly till step 3, and failing/not working on step 4 ie script is unable to check the status of the server after restart and sending a confirmation email. 该代码在步骤3之前可以正常工作,而在步骤4上失败/不起作用,即脚本在重新启动并发送确认电子邮件后无法检查服务器的状态。 Can someone please let me know where I'm going wrong. 有人可以让我知道我要去哪里了。

Try this: 尝试这个:

#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

Note: every mailx line had a trailing -$ , < /$ , or <$ -- these looked like typos and were deleted. 注意:每个mailx行的末尾都有-$< /$<$ -这些看起来像是错别字,并已被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM