19

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?

tshepang
  • 65,642
phunehehe
  • 20,240

3 Answers3

21

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.

  • 3
    that was very good, but then I may have problems with chromium, as it spawns a new process for each browser tab, anyway to resolve? – phunehehe Sep 03 '10 at 09:02
  • Edited my post to answer you. – Mike Perdide Sep 03 '10 at 09:49
  • Look like I'll need to redirect the output before the child processes are created? Thanks for the knowledge, but regarding the complexity I'd just go with nohup :) – phunehehe Sep 04 '10 at 10:23
  • Technically, this leaks duplicate file descriptors for /dev/null, but in practice that does not significantly affect anything. – Kevin Aug 20 '15 at 17:40
8

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.

Lito
  • 96
6

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.

Kevin
  • 40,767