Tuesday, November 20, 2007

“> /dev/null 2>&1″ - What does it mean?

“> /dev/null 2>&1″ - It was one of the weired commands confused me most of the times, when i looked in to the scripts especially, in make files.

It seems some cryptic stuff when you see it for the first time, and unfortunately there is no enough explanations available even in web.

wibble > /dev/null 2>&1Output redirection

The greater-thans (>) in commands like these redirect the program’s output somewhere. In this case, something is being redirected into /dev/null, and something is being redirected into &1.

Standard in, out, and error
There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR.

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.

Given that context, you can see the command above is redirecting standard output into /dev/null, which is a place you can dump anything you don’t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).

The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet!

How to kill a hung process in unix?

You can kill a hung process by giving the command

kill -9 processid

9 is the signal to the process, it allows that process to allow kill itself . remember that you can only kill processes that you have run yourself, otherwise you should have the admin privilege.