176

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?

AGamePlayer
  • 7,605

6 Answers6

266

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

Stephen Kitt
  • 434,908
garethTheRed
  • 33,957
56

/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
7ochem
  • 141
Sreeraj
  • 5,062
  • 9
    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 02:26
  • 6
    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 09:00
  • a) Yes. 2>&1 is run after the script. So, when the script runs, it writes the output (STDOUT) to /dev/null, then it sees 2>&1, so it writes the STDERR to STDOUT, which is /dev/null. That way both 2&1 (STDERR and STDOUT) ends up in /dev/null b) Standard behaviour as explained by Niels in the above comment. – Sreeraj Sep 02 '20 at 06:30
18

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

geedoubleya
  • 4,327
10

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.

phk
  • 5,953
  • 7
  • 42
  • 71
Ambrevar
  • 384
  • 2
  • 5
4

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.

chaos
  • 48,171
1

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

slm
  • 369,824
  • 1
    But cron does not necessary run Bash: the default is usually sh. In which case, &>file will fail directly from the crontab line, even before it gets to run your script (so even a shebang in the script will not have yet been invoked). – Paul_Pedant Jun 09 '20 at 22:46