Reputation: 2597
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 ?
Upvotes: 10
Views: 21823
Reputation: 4053
I find a good way is to surround the commands by parentheses, '()', (launch a sub-shell) or curly-braces, '{}' (no sub-shell; faster):
{
cmd1
cmd2
...
cmdN
} 2> error.log
Of course, this can be done on 1 line:
{ cmd1; cmd2; ... cmdN; } 2> error.log
Upvotes: 3
Reputation: 21
Two things:
2>&1
in a remote ssh command results in the error ending up inside the local tarfile, resulting in a 'broken' backup.My suggestion would be to redirect stderr
on the remote side to a file and pick it up later, in case of an error.
example:
ssh -t remotehost tar -cf - /mnt/backup 2\>backup.err > localbackup.tar
EXITSTATUS=$?
if [ $EXITSTATUS != "0" ] then
echo Error occurred!
ssh remotehost cat backup.err >localbackup.errors
cat localbackup.errors
ssh remotehost rm backup.err
else
echo Backup completed successfully!
ssh remotehost rm backup.err
fi
Upvotes: 2
Reputation: 431
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
Upvotes: 2
Reputation: 8258
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
Upvotes: 2
Reputation: 306
You could launch a new bash process redirecting the stderr of that process:
$ bash -i 2> stderr.log
$
Upvotes: 4