What does '>/dev/null 2>&1' mean in this article of crontab basics?
I am reading an article about crontab
There is something about disabling automatically sending emails.
Disable Email By default cron jobs sends an email to the user account executing the cronjob. If this is not needed put the following
command At the end of the cron job line.>/dev/null 2>&1
What is the detailed meaning for 2
>
&
and 1
? Why putting this to the end of a crontab file would turn off the email-sending thing?
Redirection Bash’s reference manual says:
The operator [n]>&word is used […] to duplicate output file descriptors
To redirect both stderr and stdout to file you should use the form
&>file
….
briefly: all STDERR and STDOUT messages will be redirect to /dev/null
This is standard I/O redirection.
There are always three default files open.
- stdin (0)
- stdout (1)
- stderr (2)
So in this example, the stdout (1
) is being redirected to /dev/null
.
The null
device is a device file that discards all data written to it.
Then stderr is then being redirected into stdout (2>&1
), therefore, both stdout and stderr will go to /dev/null
So placing this at the end of a crontab job will suppress all output and errors from the command.
Reference
>
is for redirect
/dev/null
is a black hole where any data sent, will be discarded
2
is the file descriptor for Standard Error
>
is for redirect
&
is the symbol for file descriptor (without it, the following 1
would be considered a filename)
1
is the file descriptor for Standard Out
Therefore >/dev/null 2>&1
redirects the output of your program to /dev/null
. Include both the Standard Error
and Standard Out
.
Much more information is available at The Linux Documentation Project’s I/O Redirection page.
cron
will only email you if there is some output from you job. With everything redirected to null
, there is no output and hence cron
will not email you.
Normally when cron
executes a cronjob it sends the output of the command given in the cronjob to the user account executing the cronjob. So when your cronjob executes uptime
for instance the output of uptime
is sent to the user by email.
To be clear the standard output (stdout
) of the command is meant.
Now, if you execute the command uptime
in the cornjob as follows:
uptime >/dev/null 2>&1
2>&1
means a redirection of the chanel 2 (stderr
) to the chanel 1 (stdout
). Both outputs are now on the same chanel (1
).>/dev/null
: means that the standard output (and the standard error output) is sent to/dev/null
./dev/null
is a special file:
Data written to a null or zero special file is discarded.
So, you discard the output and cron has nothing to send.
/dev/null
is a device file that acts like a blackhole. Whatever that is written to it, get discarded or disappears. When you run a script that gives you an output and if we add a > /dev/null 2>&1
at the end of the script, we are asking the script to write whatever that is generated from the script (both the output and error messages) to /dev/null
.
To break it up:
2
is the handle for standard error orSTDERR
1
is the handle for standard output orSTDOUT
2>&1
is asking to direct all the STDERR
as STDOUT
, (ie. to treat all the error messages generated from the script as its standard output). Now we already have > /dev/null
at the end of the script which means all the standard output (STDOUT
) will be written to /dev/null
. Since STDERR
is now going to STDOUT
(because of 2>&1
) both STDERR
and STDOUT
ends up in the blackhole /dev/null
. In other words, the script is silenced.
By the way, you need to have a >
in front of /dev/null 2>&1
. It should be:
x * * * * /path/to/my/script > /dev/null 2>&1
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
.