Linux shell: how to add prefix string to stderr message?
I am running several tasks simultaneously at background.
command_a | tee > command_a.log &
command_b | tee > command_b.log &
If everything is fine, then messages will be saved to log files.
If something wrong, it will print errors on screen.
But command_a and command_b are quite similar, it’s hard to tell which error message is generated by which command.
Can I add prefix string to error messages, like:
command_a: error 123
command_b: error 456
command_a: error 256
command_a: error 555
command_b: error 333
bash process substitutions:
command_a 2> >(sed 's/^/command_a: /' >&2) > command_a.log &
command_b 2> >(sed 's/^/command_b: /' >&2) > command_b.log &
You don't need to pipe to tee
if you just redirect it's stdout to a file.
You can even do fancy stuff like print a timestamp:
$ {
date
echo stdout
sleep 3
echo stderr >&2
} 2> >(gawk '{print "foobar:", strftime("[%F %T]", systime()), $0}' >&2) > file.log
foobar: [2015-02-24 11:43:32] stderr
$ cat file.log
Tue Feb 24 11:43:29 EST 2015
stdout
Stick it in a function for re-use:
log_stderr() {
gawk -v pref="$1" '{print pref":", strftime("%F %T", systime()), $0}' >&2
}
command_a > command_a.log 2> >(log_stderr "command_a")