views:

55

answers:

2

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.

+3  A: 

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?

Sjoerd
That worked. Thanks.
Martin
A: 

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

timestamp=$(date +%s)
./runServer.sh | sed "s/.*/$timestamp & /" | ....... 
ghostdog74
This obviously wouldn't work, because the date is only evaluated once.
Sjoerd
suffers the same problem of not-updated timestamp
Wrikken