In bash/ksh can we add timestamp to STDERR redirection?
E.g. myscript.sh 2> error.log
I want to get a timestamp written on the log too.
I was too lazy for all current solutions... So I figured out new one (works for stdout
could be adjusted for stderr
as well):
echo "output" | xargs -L1 -I{} bash -c "echo \$(date +'%x %T') '{}'" | tee error.log
would save to file and print something like that:
11/3/16 16:07:52 output
Details:
-L1
means "for each new line"
-I{}
means "replace {} by input"
bash -c
is used to update $(date)
each time for new call
%x %T
formats timestamp to minimal form.
It should work like a charm if stdout
and stderr
doesn't have quotes (" or `). If it has (or could have) it's better to use:
echo "output" | awk '{cmd="(date +'%H:%M:%S')"; cmd | getline d; print d,$0; close(cmd)} | tee error.log'
(Took from my answer in another topic: https://stackoverflow.com/a/41138870/868947)