Skip to content

Instantly share code, notes, and snippets.

@jasonmccallister
Last active May 6, 2020 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonmccallister/8f4eafda22e4507663a7e45242fc33f8 to your computer and use it in GitHub Desktop.
Save jasonmccallister/8f4eafda22e4507663a7e45242fc33f8 to your computer and use it in GitHub Desktop.
website-status-check
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
)
func main() {
wait := flag.Int("wait", 3, "the number of seconds to wait when checking the URL")
flag.Parse()
args := os.Args
if len(args) < 1 {
fmt.Println("you must pass a URL to monitor as the first argument")
flag.PrintDefaults()
os.Exit(1)
}
url := args[1]
var status int
for status != http.StatusOK {
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode == 200 {
status = resp.StatusCode
break
}
log.Println(fmt.Sprintf("received the response code %d from %s", resp.StatusCode, url))
time.Sleep(time.Duration(*wait) * time.Second)
}
log.Println(fmt.Sprintf("received the response code 200 from %s", url))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment