0

I want to add a timestamp to server events and store the result in a log.

My first idea was :

( ./runServer.sh ) | sed "s/.*/`date +%s` & /" | xargs -0  >Server.log 2>&1  &

But it seems sed never reevaluates the date, so all events get the same timestamp.

Now I'm trying to get around that using environment variable but I can't find a proper way to do it.

I have this obviously wrong line below :

( ./runServer.sh ) | xargs -0 'export mydate=`date +%s` ; sed "s/.*/$mydate & /"' >Server.log 2>&1  &

Any hints? Thanks.

Martin
  • 3,960
  • 7
  • 43
  • 43

2 Answers2

4

Try this:

<command> | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }'

Sauce: Is there a Unix utility to prepend timestamps to lines of text?

Community
  • 1
  • 1
Sjoerd
  • 74,049
  • 16
  • 131
  • 175
0

do it step by step, and use $() instead of backticks. Try this, not tested.

timestamp=$(date +%s)
./runServer.sh | sed "s/.*/$timestamp & /" | ....... 
ghostdog74
  • 327,991
  • 56
  • 259
  • 343