如果无法获取文件,如何自动重启Apache和Varnish?

当脚本无法获取robots.txt时,我需要重新启动Apache和Varnish,并通过电子邮件发送一些日志,但是出现错误

./healthcheck: 43 [[: not found 

我的服务器是Ubuntu 12.04 64位

 #!/bin/sh # Check if can fetch robots.txt if not then restart Apache and Varnish # Send last few lines of logs with date via email PATH=/bin:/usr/bin THEDIR=/tmp/web-server-health [email protected] mkdir -p $THEDIR if ( wget --timeout=30 -q -P $THEDIR http://website.com/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 "Web server down at " > $THEDIR/mail date >> $THEDIR/mail echo >> $THEDIR/mail echo "Apache Log:" >> $THEDIR/mail tail -n 30 /var/log/apache2/error.log >> $THEDIR/mail echo >> $THEDIR/mail echo "AUTH Log:" >> $THEDIR/mail tail -n 30 /var/log/auth.log >> $THEDIR/mail echo >> $THEDIR/mail # kick apache echo "Now kicking apache..." >> $THEDIR/mail /etc/init.d/varnish stop >> $THEDIR/mail 2>&1 killall -9 varnishd >> $THEDIR/mail 2>&1 /etc/init.d/varnish start >> $THEDIR/mail 2>&1 /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 # prepare the mail echo >> $THEDIR/mail echo "Good luck troubleshooting!" >> $THEDIR/mail # send the mail sendemail -o message-content-type=html -f [email protected] -t $EMAIL -u ALARM -m < $THEDIR/mail rm ~/.apache-was-up fi fi rm -rf $THEDIR 

 if [[ -f ~/.apache-was-up ]] 

在Ubuntu中, /bin/sh是破折号而不是bash的符号链接。 破折号是为脚本devise的最小shell,而bash更多function和devise用于交互式使用。 [[ <expression> ]]语法仅在bash中实现,而不是在短划线中,因此您会收到该错误。 虽然[ <expression> ]语法对于你所需要的是足够的,所以把这一行改为:

 if [ -f ~/.apache-was-up ] 

我想这就是你要找的东西(我更喜欢/ tmp,因为在重新启动后它被删除了,所以重新开始)。

 #!/bin/sh # Check if can fetch robots.txt if not then restart Apache and Varnish # Send last few lines of logs with date via email PATH=/bin:/usr/bin THEDIR=/tmp/web-server-health [email protected] mkdir -p $THEDIR if ( wget --timeout=30 -q -P $THEDIR http://website.com/robots.txt ) then # we are up touch /tmp/.apache-was-up else # down! but if it was down already, don't keep spamming if [ -f /tmp/.apache-was-up ]; then # write a nice e-mail echo -n "Web server down at " > $THEDIR/mail date >> $THEDIR/mail echo >> $THEDIR/mail echo "Apache Log:" >> $THEDIR/mail tail -n 30 /var/log/apache2/error.log >> $THEDIR/mail echo >> $THEDIR/mail echo "AUTH Log:" >> $THEDIR/mail tail -n 30 /var/log/auth.log >> $THEDIR/mail echo >> $THEDIR/mail # kick apache echo "Now kicking apache..." >> $THEDIR/mail /etc/init.d/varnish stop >> $THEDIR/mail 2>&1 killall -9 varnishd >> $THEDIR/mail 2>&1 /etc/init.d/varnish start >> $THEDIR/mail 2>&1 /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 # prepare the mail echo >> $THEDIR/mail echo "Good luck troubleshooting!" >> $THEDIR/mail # send the mail sendemail -o message-content-type=html -f [email protected] -t $EMAIL -u ALARM -m < $THEDIR/mail rm /tmp/.apache-was-up fi fi rm -rf $THEDIR 

虽然脚本可能工作,但也有一些问题,如不安全地使用临时文件。

你真的应该看看Monit ,它的开源,并且很容易编写一个规则,根据服务响应重启你的Web服务。 它也可以检测正在运行的进程是否死亡(例如,它的PID消失),并且可以暂时停用检查(例如,当您正在进行服务维护时)。

下面是关于如何使用Monit监视并重新启动Varnish的详细答案。

首先,请访问http://www.tecmint.com/how-to-install-and-setup-monit-linux-process-and-services-monitoring-program/获取有关Monit的一些基本信息,并安装它&#x3002; 这是一个有用的教程,因为它给出了RHEL / CentOS和Debian / Ubuntu的默认path。

其次,请访问https://www.computerminds.co.uk/articles/monitoring-varnish并按照有关设置清漆监控的说明进行操作&#x3002;

以下是我的设置中的一些注意事项:

  • 我build议使用URL / varnish-ping作为VCL ping URL。 这是一个容易记住的逻辑path。 您可以通过从命令行运行curl 127.0.0.1:80/varnish-ping并确保它返回200响应来设置一次。
  • 确保你有正确的清漆pid。 正如第二篇文章所述,你可以在init脚本中find它。 我实际上是/var/run/varnish.pid。 在Varnish运行时,您可以执行ls以确保文件存在。
  • 您可能想要访问Monitnetworking界面以帮助进行监控。 如果您有防火墙,则首先需要将端口2812添加到允许的端口。 然后,根据http://digitizor.com/how-to-enable-monit-web-interface-from-all-ips-and-change-port/更改允许访问哪些IP&#x3002; 您可以指定自己的远程IP而不是0.0.0.0/8,以获得更高的安全性。 请注意,密码是通过HTTP身份validation传递的,因此您不应使用要保持特别安全的密码。
  • 确保Monit日志处于活动状态。 检查“设置日志”日志,以确保它有像set log /var/log/monit.log
  • 如果安装Varnish并没有把它放在守护进程的列表上,以便在系统重启时启动,你将不得不通过chkconfig(或Ubuntu / Debian等价物)来添加它。 我运行chkconfig --list来确认它是否存在。

希望有助于提出一个更可靠的select。 请注意,您也可以通过Monit为服务添加电子邮件监视。 以下是关于电子邮件警报的更多详细信息: https : //fak3r.com/2010/04/10/howto-use-monit-to-monitor-sites-and-alert-users/