Linux Bash脚本:Apache服务器是否正在运行
我尝试使用bash脚本实现以下步骤: 1) 检查Apache服务器的状态 2) 如果它已启动并运行,请不要执行任何操作。如果不是,则转至步骤3 3) 如果服务器未运行,则首先发送故障电子邮件并重新启动服务器 4) 重新启动后,再次检查状态,并发送确认电子邮件 这是我的密码:Linux Bash脚本:Apache服务器是否正在运行,linux,bash,apache,Linux,Bash,Apache,我尝试使用bash脚本实现以下步骤: 1) 检查Apache服务器的状态 2) 如果它已启动并运行,请不要执行任何操作。如果不是,则转至步骤3 3) 如果服务器未运行,则首先发送故障电子邮件并重新启动服务器 4) 重新启动后,再次检查状态,并发送确认电子邮件 这是我的密码: #checking if Apache is running or not ps auxw | grep apache2 | grep -v grep > /dev/null if [ $? != 0 ] then
#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
#检查Apache是否正在运行
ps auxw | grep apache2 | grep-v grep>/dev/null
如果[$?!=0]
然后
mailx-s“Apache web服务器已关闭,正在尝试自动重新启动”-$
#web服务器关闭,请重新启动服务器
sudo/etc/init.d/apache2 restart>/dev/null
睡眠10
#检查apache是否重新启动--这不起作用
ps auxw | grep apache2 | grep-v grep>/dev/null
如果[$?=0]
然后
mailx-s“Apache成功重启”-r“$SENDEREMAIL”“$NOTIFYEMAIL”$
其他的
mailx-s“重新启动失败,请尝试手动重新启动”-r“$SENDEREMAIL”“$NOTIFYEMAIL”尝试以下操作:
#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行都有一个尾随的-$
,$
,或在开头添加“set-x”,重新运行它并添加输出。您不使用apachectl status
的具体原因是什么?我可以知道为什么要投否决票吗?这一问题遵循每一条规则,即拖延反对票并不是减少反对票的方法。在试图支配其他人的行为之前,请先熟悉堆栈溢出。我不反对任何一个堆栈溢出,如果您投票反对,请注意提供反馈。grep-v grep
是一种常见的反模式。你想要ps auxw | grep-q'[a]pache2'
或者更好的pidof apache2
我以前考虑过pidof
,信不信由你,但是在阅读手册时,如果OP的系统上存在任何可能导致其失败的许可条件,你会感到困惑。我以前从未听说过grep-vgrep,现在我查了一下,很棒的东西,Tnx!建议的代码编辑挂起。。。