le_qlg
Last Updated: February 25, 2016
·
3.622K
· jesusaurus
8b13e08e2eb0886384acfb8b46a90963

Redirect bash script to syslog

#!/usr/bin/env bash
exec > >(tee /var/log/myscript.log|logger -t myscript -s 2>/dev/console) 2>&1

The pattern exec > $X 2>&1 does two things: the last bit redirects standard error to go to standard output, and the first bit sends standard output to the file $X. Here our $X is the process substitution >( $process ) which takes the standard input of $process and presents it as a file handle. Putting these two together, we have both our standard out and our standard error going to the standard input of $process. Our $process here first runs tee, which sends its standard input to both standard out and to the file listed. We then pipe our standard out into the logger command, which sends it to syslog. The use of /dev/console is left as an exercise to the reader.

Say Thanks
Respond