Thursday, December 17, 2009

Absolutely silent wget

I had an application where I wanted to use wget to request a file from an http server, but I wanted absolutely no output. No status, no errors, nothing to the console and nothing written to the disk. This is for a laptop "anti-theft" script I'm working on. Everytime an Internet connection is established, the local script calls a PHP script on my webserver, which records the IP address it was called from. As a result, I get a nice list of IP addresses where my laptop has been. However, I don't want the disk filling up with a bunch of HTML files, as is the standard practice for wget. The solution is this:

wget http://URL -O /dev/null > /dev/null 2>&1

The "-O" directs the HTML file to /dev/null, then ">" directs the standard output to /dev/null, and then "2>&1" directs the standard error to the standard output which goes to /dev/null. As a result, this is an absolutely silent (even if its fails) wget.

One quick word of warning, the "2>&1" does not work in the sh shell. So, in your script files, instead of:

#!/bin/sh

you'll need to use another shell (like bash):

#!/bin/bash

or, not use that fancy redirect at all and use "2> /dev/null" instead.

1 comment: