I want to run a program (google-chrome
) in the background, but prevent it from outputting any messages to the terminal.
I tried doing this:
google-chrome 2>&1 1>/dev/null &
However, the terminal still fills up without messages like:
[5746:5746:0802/100534:ERROR:object_proxy.cc(532)] Failed to call method: org.chromium.Mtpd.EnumerateStorag...
What am I doing wrong? How do I redirect all the output to /dev/null
?
The operator '>' , called a redirection operator, writes data to a file. The file specified in this case is /dev/null. Hence, the data is ultimately discarded. If any other file were given instead of /dev/null, the standard output would have been written to that file.
Method 1: Single File Output Redirection“>>” operator is used for utilizing the command's output to a file, including the output to the file's current contents. “>” operator is used to redirect the command's output to a single file and replace the file's current content.
The way we can redirect the output is by closing the current file descriptor and then reopening it, pointing to the new output. We'll do this using the open and dup2 functions. There are two default outputs in Unix systems, stdout and stderr. stdout is associated with file descriptor 1 and stderr to 2.
Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator.
Redirection operators are evaluated left-to-right. You wrongly put 2>&1
first, which points 2
to the same place, as 1
currently is pointed to which is the local terminal screen, because you have not redirected 1
yet. You need to do either of the following:
2>/dev/null 1>/dev/null google-chrome &
Or
2>/dev/null 1>&2 google-chrome &
The placement of the redirect operators in relation to the command does not matter. You can put them before or after the command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With