77

I understand that this command attempts to write to nowhere or a null device but what does 2>&1 mean here?

wget -q -O - http://yourwebsite.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1
codecowboy
  • 3,442
  • 1
    Gilles found this one which seems even more like a dup: http://unix.stackexchange.com/questions/37660/order-of-redirections – slm Nov 07 '13 at 22:38

1 Answers1

127

2 refers to the second file descriptor of the process, i.e. stderr.

> means redirection.

&1 means the target of the redirection should be the same location as the first file descriptor, i.e. stdout.

So > /dev/null 2>&1 first redirects stdout to /dev/null and then redirects stderr there as well. This effectively silences all output (regular or error) from the wget command.

::edit:: Here is an excellent quick reference for you.

dg99
  • 2,642
  • Thanks. When run in a shared hosting cron job which typically sends an email of the output to an admin, would the > /dev/null 2>&1 prevent this from happening do you think? Also do spaces surrounding '>' matter? – codecowboy Nov 07 '13 at 17:28
  • Yes, the complete silencing trick is standard practice to make sure that absolutely no output exists to trigger email from a cron job. (Note, however, that the hosting provider may have done all sorts of strange things to your cron environment to make it send some sort of email regardless of your efforts.) ::edit:: And no, spaces around the lone > do not matter, but spaces within the 2>&1 probably do matter (I can't recall). – dg99 Nov 07 '13 at 17:32
  • 9
    It might also be worth noting that the same semantics are often achieved with the idiom &>/dev/null – Joseph R. Nov 07 '13 at 17:32