I have a bash script that is used for auto updating. I want to log all output of this script and prepend a timestamp to each line. I'm not sure how to add this to what I already have.
LOGFILE="logdir/update.sh.$(date +%Y-%m-%d_%H:%M).log"
exec 1>$LOGFILE 2>&1
This redirects the output of my script to a file correctly, but there is (obviously) no date added to the line. How can I add this?
For instance, I want the logged output to look like this:
[2019-11-07 1:43:45 PM]: Ign http://security.debian.org jessie/updates InRelease
I'm not looking to prepend anything to each individual command my script has, I'm looking to prepend the timestamp and log each line any one command outputs globally throughout the script.
ts
and send stdout to a file? That's obtuse. The goal is to run ./update.sh and that's it. – sc_0987 Nov 07 '19 at 19:26bash
(which supports process substitutions) you could replaceexec 1>$LOGFILE
withexec 1> >(ts > $LOGFILE)
I think – steeldriver Nov 07 '19 at 19:27