Search code examples
bashprocesstimestampstdoutstderr

BASH Prepend timestamp to stdout and stderr


I am trying to prepend a timestamp to all lines in stdout and stderr. However I do not want to redirect either of them to a file. This command is being run in a script by a process manager (pm2) which will then log stdout and stderr to their respective file. I am trying to modify the stream (without combining them) and insert the timestamp before is logged by pm2. I can get either of them working in isolation but run into issues modifying both. I appreciate any assistance you can provide.

Here is my somewhat working code

python3.6 ./bot.py > >(gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }') 2> >(gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }')

Solution

  • You need to redirect output from the second gawk command back to stderr:

    python3.6 ./bot.py \
        > >(gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }') \
       2> >(gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }' >&2)
    #                                             this part here ^^^
    

    Without that, the second gawk command is taking input from python3.6's stderr, but sending output to the same stdout as everything else.