LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Stopping cron job emails to root, do I use >/dev/null 2>&1 or &> /dev/null (https://www.linuxquestions.org/questions/linux-newbie-8/stopping-cron-job-emails-to-root-do-i-use-dev-null-2-and-1-or-and-dev-null-4175484335/)

anon091 11-12-2013 07:52 AM

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?

druuna 11-12-2013 08:40 AM

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

sag47 11-12-2013 08:40 AM

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.

anon091 11-12-2013 10:21 AM

Thanks guys. And thanks for all the details sag, that's pretty cool all the stuff you can do!


All times are GMT -5. The time now is 11:05 AM.