1748148 Members
3718 Online
108758 Solutions
New Discussion юеВ

>/dev/null 2>&1

 
SOLVED
Go to solution
'chris'
Super Advisor

>/dev/null 2>&1

hi

what does it mean

>/dev/null 2>&1

on the end of the cron job ?

kind regards
chris
2 REPLIES 2
Stuart Browne
Honored Contributor
Solution

Re: >/dev/null 2>&1

Ok, we'll need to talk a little bit about pipes and file handles to make sure this all makes sense in the long run.

First off, I'll give you the short answer:

Send any output to the bin, so you don't see it.

Now, we'll go into it a bit further, so you'll know what some of the funkie's mean.

When in a shell, you can do various things with the output and input to commands. You can run them through a pipe:

grep root /var/log/messages | less

This takes the output of the 'grep' commands, and uses the 'less' paginator to finally display it on the screen.

To go into further detail, this sends the STDOUT of 'grep' to less.

If you wanted to send the output to a file, you'd use a redirect:

grep root /var/log/messages > /tmp/file

This takes the STDOUT, and sticks it into '/tmp/file'. I won't go into detail about the > and >> options etc. as they are all covered in the man page.

There are 3 file descriptors for every process. STDIN, STDOUT and STDERR. These map to 0, 1 and 2 respecitvly.

Strangely enough, the above command can also be written as:

grep root /var/log/messages 1> /tmp/file

The first command makes the assumption that you want to redirect STDOUT. This one clears up that assumption.

Now we'll look at STDERR:

grep root /var/log/mseeagse > /tmp/file

This will spit out an error to your tty, even though you've redirected the output.

grep root /var/log/mseeagse 2> /tmp/file

This one will output nothing, and '/tmp/file' will contain the error. If we didn't miss-spell the messages file name, any errors that might occur would go to the file, but the output will go to the TTY.

Now we'll do the last confusing thing, moving a file descriptor to a different one. This is where the & comes in:

grep root /var/log/mseeagse 2> &1

This moves STDERR so it comes out in the same place as STDOUT. It says "Move file descriptor 2 to file descriptor 1".

Thus where we get to your tail. We mix and match the STDOUT and STDERR redirections:

grep root /var/log/messages > /tmp/file 2> &1
grep root /var/log/mseeagse > /tmp/file 2> &1

With both of these commands, both the regular output (STDOUT), and any errors that occur (STDERR) will go into '/tmp/file'.

When stacking redirects like this, there is one major thing to be aware of. When you move a file descriptor, you have to make sure the descriptor you are moving too has already been redirected to where you want, otherwise you can get some... interesting results.
One long-haired git at your service...
Basheer_2
Trusted Contributor

Re: >/dev/null 2>&1

Chris,

in UNIX
0 = stdin
1 = stdout
2 = stderr

>/dev/null 2>&1 means
redirect all the standard out and standard error messages/output/results from the programs/scripts to /dev/null ( which means they go to a bottomless pit)