This question already has an answer here:

I am reading an article about crontab

There is something about disabling automatically sending emails.

  1. 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?

marked as duplicate by derobert, Braiam, slm Oct 21 '14 at 16:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 165 down vote accepted

> 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 is redirect 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.

  • 1
    What if the command that I run in the crontab file do not output anything? For example something like rm -rf /home/somename/a.txt. In this case, I don't need to add that, right? – AGamePlayer Oct 21 '14 at 15:50
  • 1
    @AwQiruiGuo That's correct. A mail is delivered only when there is output on stdout or stderr. That's why they suggest to redirect both to /dev/null in the article. – moooeeeep Oct 21 '14 at 15:52
  • @moooeeeep Thanks very much for the tip! – AGamePlayer Oct 22 '14 at 4:45
  • 4
    why is fd symbol not required for 2? – Sudip Bhandari Sep 24 '16 at 10:15
  • 3
    @SudipBhandari - because there is no redirect symbol (>) before it and therefore, unlike the 1, cannot be mistaken for a filename by the shell. – garethTheRed Oct 14 '16 at 12:24

/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 or STDERR
  • 1 is the handle for standard output or STDOUT

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
  • 5
    2>&1 is run AFTER we redirect the script's O/P to /dev/null. So why does that also go to /dev/null? Also, when I do script > /dev/null why does only STDOUT go to /dev/null and not everything? – sbhatla Mar 26 '16 at 2:26
  • 2
    a) There is no sequence involved, all redirects are parsed and applied before executing the program, b) that's how the standard streams work - STDOUT and STDERR are separated for scripting, logging and analysis purposes. You may very well sometimes want to only suppress STDOUT and still receive a mail if something is sent to STDERR. – Niels Keurentjes Oct 23 '17 at 9:00

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

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.

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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.