pre-pend log timestamp

Adds a timestamp before alog from a script

awk -v sec=$(date -u +%s) '{print strftime("%Y-%m-%d %H:%M:%S",sec), $0; fflush();}'

in cron :

awk -v sec=$(date -u +\%s) '{print strftime("\%Y-\%m-\%d \%H:\%M:\%S",sec), $0; fflush();}'

So :

# echo hello world | awk -v sec=$(date -u +%s) '{print strftime("%Y-%m-%d %H:%M:%S",sec), $0; fflush();}'


2014-10-30 08:40:26 test

nJoy 🙂

3 comments to “pre-pend log timestamp”

You can leave a reply or Trackback this post.
  1. Can you update this post to remove the smart quotes and long (en/em) dash? I am having trouble using this example on CentOS 5.x and I think it is a formatting problem.


  2. CentOS 5.x comes with awk 3.1.5 which does not support the three parameter version of strftime. Removing the UTC paramter from strftime fixes the script so it runs and outputs in server local time rather than UTC.

    # echo hello world | awk -v sec=$(date –utc +%s) ‘{print strftime(“%Y-%m-%d %H:%M:%S”,sec), $0; fflush();}’
    2014-11-05 09:19:16 hello world

Write a Reply or Comment

Your email address will not be published.