tags:

views:

2964

answers:

6

Hey!

I'm looking for a way to redirect all the stderr streams in interactive bash (ideally to its calling parent process).

I don't want to redirect stderr stream from each individual command, which I could do by appending 2> a_file to each command.

By default, these stderr streams are redirected to the stdout of an interactive bash. I would like to get them on the stderr of this interactive bash process in order to prevent my stdout to be polluted by error messages and be able to treat them separatly.

Any ideas?

I still haven't found an answer ... But maybe it's actually a tty parameter. Does anybody knows something about tty/interactive shell responsibility for handling stderr ?

A: 

You could launch a new bash process redirecting the stderr of that process:

  $ bash -i 2> stderr.log
  $
caerwyn
+1  A: 

Use the exec builtin in bash:

exec 2> /tmp/myfile

gavrie
A: 

That's ok if I want to redirect it to a file. But the parent process has to be a shell because 2> syntax only works on shell. In my case it is a raw system command.

And what if I want to get it on stderr of the parent process?

Here is my use case: I'm running an interactive bash over a ssh channel to execute commands on remote servers. My ssh channel has stdin, stderr and stdout. How do I get errors of remote commands on the stderr of the SSH channel instead of stdout ?

Grégoire Cachet
A: 

Try your commands in doublequotes, like so:

ssh remotehost "command" 2>~/stderr

Tested on my local system using a nonexistant file on the remote host.

$ ssh remotehost "tail x;head x" 2>~/stderr
$ cat stderr 
tail: cannot open `x' for reading: No such file or directory
head: cannot open `x' for reading: No such file or directory
jtimberman
If you type the command directly after ssh, you aren't loading a shell on the remote host. Command is executed directly through the ssh channel.I'm using paramiko in python but this is the same. My problem is that I need an interactive shell to execute many commands and still get stderr.
Grégoire Cachet
+1  A: 

I don't see your problem it works as designed:

$ ssh remotehost 'ls nosuchfile; ls /etc/passwd' >/tmp/stdout 2>/tmp/stderr 
$ cat /tmp/stdout  
/etc/passwd 
$ cat /tmp/stderr 
nosuchfile not found
Gunstick
A: 

Tried ssh -t to create a pseudo-TTY at the remote end?

crb