When using crontab in Linux or Unix to schedule cron jobs, the cron daemon will automatically send the output of the each and every cron job to root or email address set by MAILTO variable on the cron job. The email logs all output generated by commands in the cron job which will otherwise display on the console screen.

Administrator can choose to disable email output of cron jobs by appending “> /dev/null 2>&1” to the end of command line to pipe and redirect the cron job’s output to /dev/null, a special device file that discards all data written to it without error.

However, the problem with the “> /dev/null 2>&1” is that both standard output and standard error returned by the cron jobs are ignored and discarded without email notification. The cron jobs are executed in complete silent.

To know what that “>/dev/null 2>&1” is actually doing, it’s important to understand the a little bit of concept or theory of the command.

The greater than sign (>) is meant to redirect the program’s output (that resides on the left part of sign) to another place or command (that resides on the right side of sign). In “>/dev/null 2>&1”, it’s been redirected to /dev/null. There is second part (i.e. 2>&1) which redirects 2 into &1.

To understand “2>&1”, it’s necessary to understand standard in, standard out and standard error, the three standard sources of input and output for any program or command. Standard input usually comes from keyboard for interactive program but can also receiving output from another program. The program usually prints to standard output, and sometimes to standard error in the case of exception, debug, warning or error messages.

These three data pipes are file descriptors that usually called STDIN, STDOUT and STDERR, and are numbered as 0, 1, and 2 respectively. They can be called by name, or by number. If a command does not explicitly specify a name or number, it usually refer to STDOUT, the standard output.

From that perspective, the “>/dev/null 2>&1” redirects the standard output to /dev/null to discard all standard output, and the 2 (standard error or any error message) is been redirected or treated as 1 (standard output), which means all error, warning or debug messages are also discarded or dropped. In other words, the cron job will execute without notification whatsoever, whether or not it’s completed successfully, has warning or failed. The & sign in front of 1 is standard syntax for file descriptor destination.

If you want to receive email notification in case a cron job failed to run properly, just use “>/dev/null” instead of “>/dev/null 2>&1”. “>/dev/null” will only ignore standard output, but will send all warning, debug, error and any other exception messages to root or email address specified.

For example,

* * * * * /sbin/ping -c 5 www.mydigitallife.info > /dev/null 2>&1
* * * * * /sbin/ping -c 5 www.mydigitallife.info > /dev/null

The first cron job will send no email at all, while the second cron job will notify when there is error and other debug messages occurred.

Related Posts

  • Great explanation. Thanks.
    Brazil

  • dr.ubisha

    Great explanation. Tnx

  • Ravi

    Its truly Awesome !!! Great Work!

  • IrishIwasDrinking

    Thanks, quick, simple and to the point.

  • geo

    Schnizle 🙂

  • karmafunk

    Brilliant. Just solved an issue for me. Thanks!

  • John

    Thumbs up, I always wondered about the silly looking 2>&1 in cron but never saw an explanation…

    Its nice to know WHY you’re actually doing something

  • Rich

    Fantastic explanation, clear and concise.

  • Karl

    This is well written, thanks!

  • Ravi Sharma

    Thank You..it was very helpful.

  • Ray

    Excellent description. Thanks for taking the time to put it into words.

  • LinuXplained

    Good post. I never really understood the difference between >/dev/null 2>&1 and >/dev/null. Thanks!