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:
bc. ./example.pl > /dev/null 2>&1
What happens here is as follows:
# We send standard output to /dev/null, using > /dev/null.
# 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”:http://www.oreilly.com/catalog/upt3/

2 Comments

  1. Thank you! I needed that bit quickly…

  2. Backing up Cron and Unix Command Lines

    Today I have been mostly messing with UNIX command lines. This post details the work of part of the day. I have a cron job set up which automatically backs up the crontab. Crontab is a thing which executes schedules…