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 (22.214.171.124) 56(84) bytes of data. 64 bytes from ostechnix.com (126.96.36.199): icmp_seq=1 ttl=51 time=376 ms 64 bytes from ostechnix.com (188.8.131.52): 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://ostechnix.com/
Every 1.0s: curl -I https://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://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.
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Reddit | Facebook | Twitter | LinkedIn | RSS feeds
Have a Good day!!
I wrote something along these lines years ago. It can be found here: http://stromberg.dnsalias.org/~strombrg/notify-when-up2.html .
It (by default) e-mails you and gives you an X11 popup when a state changes; it can also page you if you have an email->pager gateway.
For a website content change, you can:
notify-when-up2 –delta ‘curl –silent http://www.google.com | md5sum -‘
That’ll make it memorize the md5 hash of http://www.google.com on the first iteration, and check against that same md5 hash on each subsequent run until there’s a change.
That way you don’t have to keep checking on a watch; you can just leave it running somewhere and mostly forget about it.
That’s cool. I will check soon.
“Unlike the ping command, it will keep watching your web site status until you manually stop it. ”
The ping command will also continue monitoring unless you stop it with Ctrl-c. That is its default behavior. You have added the “-c 3” parameter which causes it to run only 3 times. Of course, on Windows the default is different, running 4 times then stopping. You can change the count with “/n number-of-runs” or make it run continually with “/t” until stopped with Ctrl-c.