What does '>/dev/null 2>&1' mean in this article of crontab basics?
>
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.
/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
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
I/O Redirection