script, rsync - - :
#!/bin/bash
set -eu
set -o pipefail
exec 1>/dev/null 2> >(logger -t "stderr-from-my-script")
: do some interesting things
rsync --del -az -e 'ssh -i mycrt.crt' /home/gnutt/backup/ me@offisite:backup
: do some other interesting things
Now all data written to stderr will be written, not just from rsync. However, ignoring stdoutit is usually a bad idea when it comes to debugging, as it may be useful information that could suggest the cause of the error. If you want to distinguish between stderr and stdout, just don't cross the threads:
exec 1> >(logger -t "stdout-from-my-script") 2> >(logger -t "stderr-from-my-script")
To explicitly close or restore descriptors, consider the following:
exec {OLD_STDOUT}>&1 {OLD_STDERR}>&2 1> >(logger -t "stdout-from-my-script") 2> >(logger -t "stderr-from-my-script")
: Do some interesting things
eval exec 1>&${OLD_STDOUT} 2>&${OLD_STDERR} ${OLD_STDOUT}>&- ${OLD_STDERR}>&-
For older versions of bash, you might need a little more stupidly:
exec 3>&1 4>&2 1> >(logger -t "stdout-from-my-script") 2> >(logger -t "stderr-from-my-script")
: Do some interesting things
exec 1>&3 2>&4 3>&- 4>&-
Craig source
share