How to redirect output and errors to /dev/null?

I have a few scripts which I’m never interested in the output from: They run in a cron job, and if they fail every once in a while, I’m not too interested in the output.

When you run said scripts or programs from the command line, the standard remedy is to add >/dev/null to redirect output to the bitbucket instead.

However, when I run these from a cron job, any output that the script would send to stderr is sent as a mail to the owner of the script, which can be a tad annoying if the script runs regularily, with output you don’t need or want, or can do anything about.

The graceful way to handle this in any shell is as follows: Redirect the output of stderr to stdout, and then redirect this combined output to /dev/null:

./example.pl > /dev/null 2>&1

What happens here is as follows:

1. We send standard output to /dev/null, using > /dev/null.
2. 2>&1 – ensures that you send the standard error (file descriptor 2), to wherever standard output (file descriptor 1) is going, which is, as already established, to the bitbucket.

Note that this is not an actual question that has showed up in my server logs — rather, this is something that I tend to forget between every time I need it, and so I have to spend time looking for it. The answers to this were found in Unix Power Tools, 3rd Edition

Leave a Reply