[Wftl-lug] stopping email from cron job
Ole Kofoed Hansen
ole+wftl at tuxino.dk
Mon Aug 13 21:49:27 EDT 2007
Gar Nelson skrev:
> Doh!
>
> * * * * * <command> > /dev/null 2>&1
>
>
> --- Gar Nelson <gar at meamil.net> wrote:
>
>> I thought the way to stop a cron job from sending me
>> an email that it did what it does was to place
>> "2>&1"
>> at the end of the cron command line. Apparently
>> not,
>> since I'm still getting mail. Not sure what I'm
>> missing.
Obviously you found the solution you needed, but for the benefit of the
rest of the list, I'll just give a bit more explanation.
First of all, you should know that all programs running on any UNIX-like
system starts with three files open.
0) stdin - for input
1) stdout - for normal output
2) stderr - for error messages
For a program, which is called normally on a terminal, all three files
point to the terminal, so that the program will ask for input on the
terminal, and print both normal output and errors there as well. But it
is often useful to redirect these, and luckily the shell allows this.
Before I start with the examples, I would like to note that these are
just examples supposed to make the concept easily understood. In most of
the examples, redirection is not really needed to achieve the result of
the example command.
It is done with three special characters: < > and |
To test it, start with running the command cat without any parameters.
It will read each line of input from its stdin and output that line to
its stdout, until it gets an end-of-file marker on stdin. Type Ctrl-D to
end the input.
Next test would be to redirect to a text file, which is done with the >
character. Notice that it looks similar to a funnel.
cat > filename
That command will read your input from the terminal just like the
previous, but instead of sending the ouput to the terminal, it will be
put into the file. Please note, that if the file already existed, it
will be overwritten. If you want to append to the end of it instead, you
must use >> instead of >. This is often more efficient than starting a
text editor, if you just need to create a new text file with a couple of
lines, or append a line to an existing file.
By turning the funnel around, you make the data go the other way.
cat < filename
This time the command reads the file and outputs it to the terminal.
While it looks as if it does exactly the same as if the < had not been
there, it is actually not the same. The difference is that with the <
present, the cat program has no knowledge of where the data comes from,
it is just stdin. It is the shell that opens the file.
The last character | - usually called the pipe character - can make one
program's output become the input of another program. I can't think of a
sensible example with just cat, but lets add sort to it.
cat | sort
This will sort the input lines alphabetically (if you try with numbers,
the result may not be quite what you expect). Redirecting can also be
combined as in:
cat < filename | sort
This is an especially contrived example. If you really want to sort a
file, just type sort filename.
The previous examples only dealt with standard in and out. Lets try one
that should give a bunch of normal output as well as an error.
cat /etc/passwd /etc/shadow
If run as a normal user, that should print out the entire password file,
and an error message telling that you're not allowed acces to the shadow
file. Now suppose that you wanted to save all the output from a command
to a file. If you tried that with this one, only the password file would
get saved. The error message would not. The solution is to use 2>&1
which tells stderr to go to the same place that stdout goes. See the
examples below for the order - it is important to order the redirections
correctly.
In some cases you might also want to just throw away the output. This
can be done by redirecting it to /dev/null. If you only redirect stdout,
it will still ouput any error messages. I'll end this with a couple more
examples.
cat /etc/passwd /etc/shadow > /dev/null
Throws away normal output, but not error messages.
cat /etc/passwd /etc/shadow 2> /dev/null
Throws away error messages, but not normal output.
cat /etc/passwd /etc/shadow 2>&1 > /dev/null
It may look like the 2>&1 has no effect.
cat /etc/passwd /etc/shadow > /dev/null 2>&1
Gives no output and no errors.
(cat /etc/passwd /etc/shadow > /dev/null) > filename
(cat /etc/passwd /etc/shadow 2>&1 > /dev/null) > filename
These two show that 2>&1 really had an effect. It made the command put
its errors on stdout, but after throwing out the normal output.
Hope this helps some people understand redirection more. It is a really
useful tool.
Regards
Ole
More information about the Wftl-lug
mailing list