Jump to content
  • 0

How do I do a website health check using CURL command


Question

I'm trying to monitor a website using curl but the output doesn't seem to work, please see commands below:

#!/bin/bash

varDate=$(date '+%Y-%m-%d %H:%M:%S')
varCurlError=$(curl -sSf https://website.com > /dev/null)
varHttpCode=$(curl -Is https://website.com | head -n 1)
varResponseTime=$(curl -s -w '%{time_total}' -o /dev/null website.com)
varOutput="$varDate | $varCurlError | $varHttpCode | $varResponseTime"
echo $varOutput

The output looks like this :

 | 0.07323 18:51:40 | | HTTP/1.1 200 OK

What can I change or add to fix the output.

Much appreciated.

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0
2 hours ago, Johann said:

I'm trying to monitor a website using curl but the output doesn't seem to work, please see commands below:


#!/bin/bash

varDate=$(date '+%Y-%m-%d %H:%M:%S')
varCurlError=$(curl -sSf https://website.com > /dev/null)
varHttpCode=$(curl -Is https://website.com | head -n 1)
varResponseTime=$(curl -s -w '%{time_total}' -o /dev/null website.com)
varOutput="$varDate | $varCurlError | $varHttpCode | $varResponseTime"
echo $varOutput

The output looks like this :


 | 0.07323 18:51:40 | | HTTP/1.1 200 OK

What can I change or add to fix the output.

Much appreciated.

 

Hi Johann, 

You should use timestamps when calculating date-time differences. 

 

Adjust your code like this:

#!/bin/bash

website="$1"

startTimestamp=$(date '+%s.%N')
homepageContent=$(curl -sSf "$website" > /dev/null)
endTimestamp=$(date '+%s.%N')
diffTimestamp=$(python3 -c "print('%.2f' % ($endTimestamp - $startTimestamp))")
echo "$website - Took $diffTimestamp seconds"

image.png.b442388727b22b6625baa4a073c2a2b6.png

 

The date-time differences should display correctly.

image.png.2c56357275e66af63ce087f2d7d64b04.png

 

I hope this helps.

Shahriar Shovon

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...