在linux中获取网站的状态码

我有一个小vps我主持一个我开发的networking应用程序,并开始接受大量的访问。

我需要检查/validation一下,每隔X分钟看networking是否启动并运行(检查状态代码,200)还是如果closures(代码500),如果closures,然后重新运行一个脚本,我使重新启动一些服务。

任何想法如何检查在Linux? 卷毛,山猫?

curl --head --max-time 10 -s -o /dev/null \ -w "%{http_code} %{time_total} %{url_effective}\n" \ http://localhost 

10秒后超时,并报告响应码和时间

如果请求超时(检查$?),Curl将退出,出错代码为28

在姊妹网站(serverfault)上找到这个https://serverfault.com/questions/124952/testing-a-website-from-linux-command-line

 wget -p http://site.com 

这似乎是诀窍

对于这样的问题,工具的man页通常提供所有可能选项的很好的列表。

curl你也可以在这里找到它。

你似乎搜索的选项是-whttp-code variable

编辑:

请看@ Ken的如何使用-w的回答。

好的,我创建了两个脚本:

  1. site-statush.sh http://yoursite.com =>检查站点状态,如果200,不要做任何事情,否则调用services-action.sh restart
  2. services-action.sh restart =>重新启动$ services中指定的所有服务

查看https://gist.github.com/2421072