Friday, January 23, 2015

Regarding STDERR STDOUT STDIN


crontab -l
sh /root/ntp.sh  >>/dev/null 2>&1

There are three standard sources of input and output for a program

> STDIN     :0
> STDOUT :1
> STDERR : 2

Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2,
in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.



Ques :
 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



Ans :

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


AS WELL AS ,

/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:

* * * * * /path/to/my/script > /dev/null 2>&1

No comments:

Post a Comment