Created
December 18, 2013 14:38
-
-
Save joekr/8023373 to your computer and use it in GitHub Desktop.
Watch Dog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "net/http" | |
import "time" | |
import "os" | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Println("Need a url", len(os.Args)) | |
return | |
} | |
url := os.Args[1] | |
ticker := time.NewTicker(time.Second * 300) | |
for t := range ticker.C { | |
checkSite(t, url) | |
} | |
} | |
func checkSite(t time.Time, url string) { | |
resp, err := http.Get(url) | |
f, err := os.OpenFile("log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) | |
if err != nil { | |
panic(err) | |
} | |
defer f.Close() | |
const layout = "Jan 2, 2006 at 3:04pm (MST)" | |
f.WriteString("Ran at " + t.Format(layout) + " - ") | |
f.WriteString(resp.Status + "\n") | |
if err != nil { | |
// handle error | |
fmt.Println(err) | |
} | |
resp.Body.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment