An Easy Way To Monitor A Website From Command Line In Linux
This brief tutorial explains a simplest method to monitor a website from command line in Unix-like systems. We all know that ping command will quickly tell you whether a host or website is up or down. Usually, most of us check whether a website is up or down like below using command:
$ ping ostechnix.com -c 3
PING ostechnix.com (184.108.40.206) 56(84) bytes of data. 64 bytes from ostechnix.com (220.127.116.11): icmp_seq=1 ttl=51 time=376 ms 64 bytes from ostechnix.com (18.104.22.168): icmp_seq=2 ttl=51 time=374 ms --- ostechnix.com ping statistics --- 3 packets transmitted, 2 received, 33% packet loss, time 2000ms rtt min/avg/max/mdev = 374.828/375.471/376.114/0.643 ms
But, Would you run this command every time to check whether your website is up or down? Of course, you might create a script to check your website status at periodic intervals. However, it is not necessary! Here is simple one-liner command that will watch or monitor on a regular interval:
$ watch -n 1 curl -I http://DOMAIN_NAME/
For those who don’t know, watch command is used to run any command in a particular intervals.
Monitor A Website From Command Line In Linux
Let us check if ostechnix.com site is live or down. To do so, run:
$ watch -n 1 curl -I https://www.ostechnix.com/
Every 1.0s: curl -I https://www.ostechnix.com/ sk: Thu Dec 22 17:37:24 2016 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0 HTTP/1.1 200 OK Date: Thu, 22 Dec 2016 12:07:09 GMT Server: ApacheD Vary: Cookieh Link: <https://www.ostechnix.com/wp-json/>; rel="https://api.w.org/", <https://w p.me/5ILHv>; rel=shortlinki Content-Type: text/html; charset=UTF-8
The above command will monitor our site ostechnix.com at every one second interval. You can change the monitoring time as you wish. Unlike the ping command, it will keep watching your web site status until you manually stop it. To stop this command, press CTRL+C.
If you got HTTP/1.1 200 OK in the output, great! It means your website is working and live.
What about google?
$ watch -n 1 curl -I https://www.google.co.in/
Great! Google is working!
Facebook? Just run:
$ watch -n 1 curl -I https://www.facebook.com/
Well, Facebook is up and running too!
If you have a local web server running , you can cross check it by stopping your web server manually running the following commands,
If your website is running with nginx, run:
$ sudo systemctl stop nginx
If your website is using Apache, then you can stop as shown below.
$ sudo systemctl stop httpd
Now, go and check the command’s output. You will see connection refused error. Quite handy, isn’t it?
This is a really cool one-liner command that you can use to monitor sites from Terminal. Also, It is quite useful when moving DNS or any other types of site migrations. It really comes in handy during high traffic events where the site going down is a strong possibility.
I know I know, this method is not recommended for monitoring production and mission critical sites. There are so many free or paid tools, software, and apps are available to watch and monitor such kind of sites. Those tools will immediately send alerts when your site is down. However, the above one-liner command is a quickest way to check the status of your local or public website within seconds.