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? |
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 |
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:
In the second case, Code:
>/dev/null 2>&1 Quote:
Now let's say you reverse the order... Code:
2>&1 1>/dev/null 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 Code:
curl -v http://www.google.com 1> /dev/null Code:
curl -v http://www.google.com 2>&1 1> /dev/null | grep '^< \|^> ' |
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. |