The Curious Dev

Various programming sidetracks and shiny-object detours

URL Status Check

Groovy is great for those little tasks where you probably can’t be bothered pulling out your Java tooling.

While certainly more useful as part of a larger collection of tools, the script below lets you check if a website is up by simply downloading that page and confirming that there are greater than zero lines of content.

//saved as URLCheckUtil.groovy
def url = args[0]
g = new URLChecker()
def status = g.checkURL(url)
println "Status for ${url} => ${status}"

class URLChecker {
    static int checkURL(address) {
        def status = 0
        //println "checkingURL('${address}')"

        try {
            def url = new URL(address)
            if (url.readLines().size() > 0) {
                status = 1
        } catch (FileNotFoundException fnfe) {
            //nothing, typically means the URL is wrong, status => 0
        } catch (ConnectException ce) {
            //nothing, typically means the URL is wrong
            // ... OR the server we're expecting to be there is DOWN, status => 0
        } catch (IOException ioe) {
            //some other kind of issue occurred, let this one through as ok.
            status = 1

        return status

This can be executed with something like this:

c:\>groovy URLCheckUtil.groovy
Status for => 1

This script utilises the simpler (than Java) syntax of a URL and you can simply call readLines on the URL object. This returns a List of the lines, which you can then just count with the size method.

If you wanted to actually parse the content, you could iterate over the lines with eachLine in a closure, such as:

url.eachLine { ln ->
    println ln
    //do some work here...


The script is here


Included file 'facebook_like.html' not found in _includes directory