How to redirect output of a running program to /dev/null

I know that in order to suppress the output of a program I can redirect it to /dev/null, for example to hide all error and warning messages from chromium I can start the program like this

chromium-browser 2> /dev/null &

However, if I happen to forget about the error messages and type

chromium-browser &

(which is quite annoying when they appear in the middle of a command) I don’t know what to do except for stopping the application and starting it again properly.

Can I somehow redirect error output without restarting the application?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This was answered here : here by vladr.
The answer is (quoting) :

  • attach to the process in question using gdb, and run:
  • p dup2(open(“/dev/null”, 0), 1) (for stdout redirection)
  • p dup2(open(“/dev/null”, 0), 2) (for stderr redirection)
  • detach
  • quit

I tried it on the following script :
[edition after first comment :]

sleep 10 # so I can have the time to attach to the process
if [ "$sonorfather" == "father" ] # avoid infinite recursion 
then 
   sonorfather=son ./test & 
fi 

while true 
do 
   echo "stdout $sonorfather" 
   echo "stderr $sonorfather" >&2 
   sleep 1 
done 

I disabled the stderr output before the son process was created, here is the output :

stdout father 
stdout son 
stdout father 
stdout son 
[and so on...]. 

I hope this answer your question : the son process stderr was redirected too.

Method 2

You can also launch the browser with nohup and then close the terminal window with the following:

nohup chromium-browser &

This way, the browser will launch and detach from the console, that can then be closed quietly.

Method 3

You could also setup an alias for chromium-browser to instead run chromium-browser 2> /dev/null

e.g. if you are using bash, edit /home/username/.bashrc and add line:

chromium-browser='chromium-browser 2> /dev/null'

or better yet

chrome='chromium-browser 2> /dev/null'

and save some keystrokes.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x