Bash Logging
tl;dr:
exec 1> >(logger -s -t "$(basename "$0")") 2>&1
Been looking for a way to pipe your entire bash script into a log file, syslog, journald, or whatever? The above line does it super simply!!
This was a line I had found a few years back, started learning ansible, and lost this nugget of knowledge, and recenlty found it once again (but lost the website I found it from… Sorry!)
What does it do?
exec
: this line tells bash “you are to run everything”1> >(
this sets up piping1
(stdout
) through to whats inside the parenthesislogger -s -t "$(basename "$0")")
thelogger
program is a means of piping output into your systemssyslog
orrsyslog
or what have you.basename
adds the file/scripts name onto the syslog entry.2>&1
is for piping2
orstderr
into theexec
command
This information was wonderfully pulled from UrbanAutomation’s website.