Linux shell: how to add prefix string to stderr message?

3

3

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

Fisher

Posted 2015-02-24T16:23:58.020

Reputation: 289

Answers

5

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")

glenn jackman

Posted 2015-02-24T16:23:58.020

Reputation: 18 546

For my understanding, 2> redirect all stderr, >() is redirect to a command, sed used to add prefix, after sed message become stdout, >&2 convert message back to stderr, finally save to log file. My understanding is correct? Thanks! @glenn jackman – Fisher – 2015-02-24T18:41:58.037

yes, except >() is called a "process substitution" – glenn jackman – 2015-02-24T18:42:43.373

@Fisher you may want to accept an answer if it answers your question (that's the [so] way of saying "Thanks") – umläute – 2017-05-29T09:18:37.503