bash bash脚本自动重启Apache
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2168518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash script to restart Apache automatically
提问by usef_ksa
I wrote a bash script to restart Apache when it hanged and send email to the admin. The code is shown below. the code will restart Apache if the number of Apache process is zero. The problem is: Apache some time hangs and processes is still not zero,so in this case the script will not restart Apache. The needed is: how do I modify the code to restart Apache if it hanged and the processes is not zero.
我编写了一个 bash 脚本来在 Apache 挂起时重新启动它并向管理员发送电子邮件。代码如下所示。如果 Apache 进程数为零,代码将重新启动 Apache。问题是:Apache 有一段时间挂起并且进程仍然不为零,因此在这种情况下脚本不会重新启动 Apache。需要的是:如果 Apache 挂起并且进程不为零,我该如何修改代码以重新启动 Apache。
#!/bin/bash
if [ `pgrep apache2 -c` -le "0" ]; then
/etc/init.d/apache2 stop
pkill -u www-data
/etc/init.d/apache2 start
echo "restarting....."
SUBJECT="Apache auto restart"
# Email To ?
EMAIL="[email protected]"
# Email text/message
EMAILMESSAGE="apache auto restart done"
# send an email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" "$EMAILMESSAGE"
fi
回答by Thomas
We used to have Apache segfaulting sometimes on a machine; here's the script we used trying to debug the problem while keeping Apache up. It ran from cron (as root) once every minute or so. It should be self-explanatory.
我们曾经有时在机器上使用 Apache 段错误;这是我们用来在保持 Apache 正常运行的同时调试问题的脚本。它每分钟左右从 cron(以 root 身份)运行一次。它应该是不言自明的。
#!/bin/sh
# Script that checks whether apache is still up, and if not:
# - e-mail the last bit of log files
# - kick some life back into it
# -- Thomas, 20050606
PATH=/bin:/usr/bin
THEDIR=/tmp/apache-watchdog
[email protected]
mkdir -p $THEDIR
if ( wget --timeout=30 -q -P $THEDIR http://localhost/robots.txt )
then
# we are up
touch ~/.apache-was-up
else
# down! but if it was down already, don't keep spamming
if [[ -f ~/.apache-was-up ]]
then
# write a nice e-mail
echo -n "apache crashed at " > $THEDIR/mail
date >> $THEDIR/mail
echo >> $THEDIR/mail
echo "Access log:" >> $THEDIR/mail
tail -n 30 /var/log/apache2_access/current >> $THEDIR/mail
echo >> $THEDIR/mail
echo "Error log:" >> $THEDIR/mail
tail -n 30 /var/log/apache2_error/current >> $THEDIR/mail
echo >> $THEDIR/mail
# kick apache
echo "Now kicking apache..." >> $THEDIR/mail
/etc/init.d/apache2 stop >> $THEDIR/mail 2>&1
killall -9 apache2 >> $THEDIR/mail 2>&1
/etc/init.d/apache2 start >> $THEDIR/mail 2>&1
# send the mail
echo >> $THEDIR/mail
echo "Good luck troubleshooting!" >> $THEDIR/mail
mail -s "apache-watchdog: apache crashed" $EMAIL < $THEDIR/mail
rm ~/.apache-was-up
fi
fi
rm -rf $THEDIR
We never did figure out the problem...
我们一直没有弄清楚问题...
回答by Paused until further notice.
Can the count of a process really be less than zero?
进程的计数真的可以小于零吗?
This should be sufficient:
这应该足够了:
if ! pgrep apache2 -c >/dev/null; then
回答by tangens
You could try to send an http request to apache (e.g. using wget --timeout=10
) and if that request times out or fails (exit status != 0), you kill and restart apache.
您可以尝试向 apache 发送 http 请求(例如使用wget --timeout=10
),如果该请求超时或失败(退出状态!= 0),您将终止并重新启动 apache。
回答by Dirk Eddelbuettel
Why would Apache hang? Can you get to the cause?
为什么 Apache 会挂起?你能找到原因吗?
There are a number of scripts and tools out there to 'daemonize' apps and watch over them. As you seem to be on Debian or Ubuntu, have a look at the packages daemon
and daemontools
. I am sure there are others too.
有许多脚本和工具可以“守护”应用程序并监视它们。由于您似乎在使用 Debian 或 Ubuntu,请查看软件包daemon
和daemontools
. 我相信还有其他人。