From the manual cron(8):
When executing commands, any output is mailed to the owner of the crontab […].
So what your article suggests here is to produce no output, thus sending no mail.
Another way (more convenient?) to disable mail is to use the -m off
option, i.e.
crond -m off
Now to the syntax: this is specific to the Bourne shell language (and its derivatives such as bash
, zsh
, and so on).
[n]>file
[n]>fd
will redirect to file descriptor n
(or standard output if unspecified) to file descriptor fd
.
A file descriptor can be a file name of the address of a stream. &
is the address operator as in the C language.
Conventionally, file descriptor 1
is standard output (a.k.a. stdout) and file descriptor 2
is standard error (a.k.a. stderr). The chunk
>/dev/null
is redirecting stdout to /dev/null.
'2>&1'
is redirecting the error stream to the output stream, which has been redirected to /dev/null. As such, no output is produced and no mail is sent.
Warning: the order of redirection matters:
>/dev/null 2>&1
is not the same as
2>&1 >/dev/null
Try these two commands with a non-privileged user:
ls >/dev/null 2>&1
ls 2>&1 >/dev/null
Indeed, in the later case, file descriptor 2
is set to the current address of file descriptor ``1 (which is stdout at this very moment), and then the file descriptor 1
is redirected to /dev/null
. File descriptor 2
is still redirected to stdout, no matter what happens to file descriptor 1
.
rm -rf /home/somename/a.txt
. In this case, I don't need to add that, right? – AGamePlayer Oct 21 '14 at 15:50/dev/null
in the article. – moooeeeep Oct 21 '14 at 15:52> /dev/null
? – IgorGanapolsky Aug 15 '16 at 21:50>
) before it and therefore, unlike the1
, cannot be mistaken for a filename by the shell. – garethTheRed Oct 14 '16 at 12:24& > /dev/null
? As I read here&>filename # Redirect both stdout and stderr to file "filename."
– genderbee Oct 22 '19 at 11:54