Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-12-2013, 07:52 AM   #1
Senior Member
Registered: Jun 2009
Posts: 1,795

Rep: Reputation: 49
Stopping cron job emails to root, do I use >/dev/null 2>&1 or &> /dev/null

I know this is probably a REAL basic question, but I'm not sure what the difference is between

>/dev/null 2>&1
&> /dev/null

in terms of having a cron job not send an email to root for the cron'd command. Is one an "old" way vs. a "new" way, or something like that?
Old 11-12-2013, 08:40 AM   #2
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401
If the command(s) you use in cron need stderr and/or stdout redirection then you should create a script. Let the script do all redirecting in a controlled way. Run the script from cron.

If a command creates output (error or normal) and it is launched from cron then cron will mail you the, for cron, unexpected output. You might want to have a look at the crontab manual page (man 5 crontab) and search for MAILTO. I personally don't touch the MAILTO variable (I like to know when something cron related goes wrong).

Here are 2 links about redirecting:
- How To Redirect stderr To stdout
- All about redirection
1 members found this post helpful.
Old 11-12-2013, 08:40 AM   #3
Senior Member
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,861
Blog Entries: 36

Rep: Reputation: 459Reputation: 459Reputation: 459Reputation: 459Reputation: 459
The short answer. You can use either method to accomplish discarding output of your cron job. Here is a more thorough description describing the differences between the two ways of discarding output.

The end result is they both accomplish the same thing (redirecting both file descriptor 1 and 2 to /dev/null). However each method goes about it slightly differently. Both ways have been around for as long as I remember. For your reference read the bash man page on Redirection.

&> /dev/null
Originally Posted by Bash man page section 3.6.4
There are two formats for redirecting standard output and standard error:
Of the two forms, the first is preferred. This is semantically equivalent to
>word 2>&1
You're basically redirecting both file descriptor 1 (previously stdout) and file descriptor 2 (previously stderr) to write a text stream to /dev/null. You're essentially setting them both without depending on one another.

In the second case,

>/dev/null 2>&1
Originally Posted by Bash man page section 3.6
Note that the order of redirections is significant. For example, the command
ls > dirlist 2>&1
directs both standard output (file descriptor 1) and standard error (file descriptor 2) to the file dirlist, while the command
ls 2>&1 > dirlist
directs only the standard output to file dirlist, because the standard error was made a copy of the standard output before the standard output was redirected to dirlist.
Basically, you're telling file descriptor 2 (previously stderr) to set the output stream to write to the same location as file descriptor 1 (previously stdout but currently /dev/null) but once they're set they're independent of each other (in your case both file descriptors would be set to /dev/null).

Now let's say you reverse the order...

2>&1 1>/dev/null
You just hooked up file descriptor 2 to the current value of file descriptor 1 (currently stdout). Then you set file descriptor 1 (previously stdout) to output to /dev/null. So now what would normally be output to stderr is now being output to stdout and what would normally be output to stdout is discarded to /dev/null. Doing something like this is useful if you want to process stderr by other utilities.

A good example of this would be curl and looking at the REQUEST and RESPONSE http headers in one line.

curl output both stout and stderr normally (shows both HTTP headers and the response body plus some other meta info).

curl -v
curl discard output only showing stderr (notice that the headers' lines start with either '< ' or '> ').

curl -v 1> /dev/null
curl hook stderr up to stdout and discard output from file descriptor 1. Use grep to show only the headers.

curl -v 2>&1 1> /dev/null | grep '^< \|^> '
As you can see from your original question that both accomplish essentially the same thing. However the latter example is flexible and can be used in very interesting ways to manipulate output from the utilities when forming one liner command filters.

Last edited by sag47; 11-12-2013 at 08:51 AM.
1 members found this post helpful.
Old 11-12-2013, 10:21 AM   #4
Senior Member
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
Thanks guys. And thanks for all the details sag, that's pretty cool all the stuff you can do!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cron Error: Cron <EMAIL> /tmp/.X11-unix/.bash/update >/dev/null 2>&1 (failed) willrocks Linux - Server 8 01-20-2012 12:08 AM
>/dev/null 2>&1 cccc Linux - Server 3 08-28-2007 06:13 AM
Diff Between /dev/zero & /dev/null rajaniyer123 Solaris / OpenSolaris 4 07-02-2007 06:09 AM
What is meant by " file > /dev/null 2>&1 </dev/null " attockonian Linux - Newbie 5 06-30-2006 10:51 PM
> /dev/null 2>&1 Sammy2ooo Linux - Software 2 07-16-2003 09:20 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:34 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration