Last active
December 6, 2019 08:19
-
-
Save ConsoleCatzirl/5250497 to your computer and use it in GitHub Desktop.
Redirect a bash script's output and error to syslog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# 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 | |
exec > >(tee /var/log/myscript.log|logger -t myscript -s 2>/dev/console) 2>&1 | |
# The bulk of the script goes here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment