views:

168

answers:

2

Im writing a bash-script to perform an offsite backup, using rsync over SSH. I'm able to send STDOUT to logger, for logs via

rsync --del -az -e 'ssh -i mycrt.crt' /home/gnutt/backup/ me@offisite:backup | logger -i

But I want to send STDERR instead, so if there is a problem, such as that offsite is unavailable, that output should be sent to logger and logged.

+4  A: 

You can redirect the STDERR descriptor (2) to STDOUT (1) by adding 2>&1, for example:

rsync --del -az -e 'ssh -i mycrt.crt' /home/gnutt/backup/ me@offisite:backup  2>&1 | logger -i
David Gelhar
James Morris
@James right; editing to clarify that point
David Gelhar
+1  A: 

If you want stderr instead of stdout (rather than stderr AND stdout), you can do the following:

  1. open another file descriptor (9)
  2. redirect stdout (1) to the new file descriptor (9)
  3. redirect stderr (2) to stdout (1)

Which looks like this:

rsync --del -az -e 'ssh -i mycrt.crt' /home/gnutt/backup/ me@offisite:backup 9> /dev/null 1>&9 2>&1 | logger -i

Alternately, you could employ process substitution:

logger -i <( rsync --del -az -e 'ssh -i mycrt.crt' /home/gnutt/backup/ me@offisite:backup > /dev/null )
Patrick