{"id":499,"date":"2011-09-11T11:00:48","date_gmt":"2011-09-11T16:00:48","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=499"},"modified":"2011-09-11T11:01:38","modified_gmt":"2011-09-11T16:01:38","slug":"monitoring-web-server-status-with-a-shell-script","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2011\/09\/monitoring-web-server-status-with-a-shell-script\/","title":{"rendered":"Monitoring web server status with a shell script"},"content":{"rendered":"<p>Recently, my VPS (Virtual Private Server) ran into some issues where it exceeded the maximum amount of RAM allotted under my subscription. When this happens, the web server software shuts down and does not restart until I manually restart it.<\/p>\n<p>This is bad. I&#8217;m not always visiting my own web site, so it could be down for days without me knowing. Although I really need to identify what is using all the RAM, in the meantime I&#8217;ll settle for a monitoring system that will notify me when the server is down.<\/p>\n<pre><code>#!\/bin\/bash\r\nif curl -s --head http:\/\/osric.com\/ | grep \"200 OK\" &gt; \/dev\/null\r\n  then \r\n    echo \"The HTTP server on osric.com is up!\" &gt; \/dev\/null\r\n  else\r\n    echo \"The HTTP server on osric.com is down!\"\r\nfi<\/code><\/pre>\n<p><a href=\"http:\/\/curl.haxx.se\/\" title=\"cURL\">cURL<\/a> will let you retrieve a URL via the command line, and provides more options than <a href=\"http:\/\/www.gnu.org\/s\/wget\/\" title=\"Wget\">Wget<\/a> for a single URL. In this case, I used the <em>silent<\/em> switch to eliminate the status\/progress output, and the <em>head<\/em> switch to retrieve only the document headers. The document header is then piped to <a href=\"http:\/\/www.gnu.org\/s\/grep\/\" title=\"Grep\">Grep<\/a>, which searches for the string &#8220;200 OK&#8221; (the <a href=\"http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html#sec10.2.1\" title=\"HTTP status message 200 OK\">HTTP status message for a successful request<\/a>).<\/p>\n<p>I send the result of that to \/dev\/null so that the output doesn&#8217;t appear on the screen.<\/p>\n<p>If grep does find 200 OK, then I send a success message to \/dev\/null. This is largely unnecessary, but it is nice to leave in to test the script in a successful case&#8211;just remove the <code>&gt; \/dev\/null<\/code>. If it doesn&#8217;t find 200 OK, then there is a problem. It might not mean, necessarily, that the web server is down, but it definitely indicates there is a problem that needs to be identified.<\/p>\n<p>I added a call to this script to a crontab to run every 5 minutes. If there is no output, nothing happens. If there is output, the output is sent to me via e-mail, which, assuming I am checking my e-mail religiously, should reduce server downtime.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, my VPS (Virtual Private Server) ran into some issues where it exceeded the maximum amount of RAM allotted under my subscription. When this happens, the web server software shuts down and does not restart until I manually restart it. This is bad. I&#8217;m not always visiting my own web site, so it could be &hellip; <a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2011\/09\/monitoring-web-server-status-with-a-shell-script\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Monitoring web server status with a shell script<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[73,197,216,215,217,218],"class_list":["post-499","post","type-post","status-publish","format-standard","hentry","category-best-practices","tag-apache","tag-bash","tag-cron","tag-curl","tag-monitoring","tag-web-server"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=499"}],"version-history":[{"count":3,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/499\/revisions"}],"predecessor-version":[{"id":510,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/499\/revisions\/510"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}