Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending STDERR to logger

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.

like image 219
Gnutt Avatar asked Mar 26 '10 14:03

Gnutt


People also ask

How do I send stderr to stdout?

Understanding the concept of redirections and file descriptors is very important when working on the command line. To redirect stderr and stdout , use the 2>&1 or &> constructs.

Can stderr be redirected?

The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.

What is the difference between stdout and stderr?

stdout − It stands for standard output, and is used to text output of any command you type in the terminal, and then that output is stored in the stdout stream. stderr − It stands for standard error. It is invoked whenever a command faces an error, then that error message gets stored in this data stream.


2 Answers

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
like image 173
David Gelhar Avatar answered Sep 18 '22 01:09

David Gelhar


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 )
like image 26
Patrick McMurchie Avatar answered Sep 20 '22 01:09

Patrick McMurchie