LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 11-12-2013, 08:52 AM   #1
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Rep: Reputation: 46
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
-or-
&> /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, 09:40 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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, 09:40 AM   #3
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,831
Blog Entries: 36

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
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.

Code:
&> /dev/null
Quote:
Originally Posted by Bash man page section 3.6.4
There are two formats for redirecting standard output and standard error:
&>word
and
>&word
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,

Code:
>/dev/null 2>&1
Quote:
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...

Code:
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).

Code:
curl -v http://www.google.com
curl discard output only showing stderr (notice that the headers' lines start with either '< ' or '> ').

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

Code:
curl -v http://www.google.com 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 09:51 AM.
 
1 members found this post helpful.
Old 11-12-2013, 11:21 AM   #4
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

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


Reply


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 01:08 AM
>/dev/null 2>&1 cccc Linux - Server 3 08-28-2007 07:13 AM
Diff Between /dev/zero & /dev/null rajaniyer123 Solaris / OpenSolaris 4 07-02-2007 07:09 AM
What is meant by " file > /dev/null 2>&1 </dev/null " attockonian Linux - Newbie 5 06-30-2006 11:51 PM
> /dev/null 2>&1 Sammy2ooo Linux - Software 2 07-16-2003 10:20 AM


All times are GMT -5. The time now is 10:20 AM.

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