0

I've found this script (How to add timestamp to STDERR redirection) to redirect stdout/stderr of a script/program and add a time stamp:

#!/bin/bash
while read line ; do
    echo "$(date): ${line}"
done

when using this script (predate.sh) like

./MyApp | predate.sh > out.log

it works great, but when MyApp crashes or gets killed, the predate.sh script continuously runs and does not terminate. Does anyone know how to solve that?

Community
  • 1
  • 1
rico
  • 61
  • 1
  • 4

2 Answers2

1

There may be a buffering issue. I think this will work for you:

producer | stdbuf -oL gawk '{print strftime("%F %T"), $0}' > out.log
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • thanks, unfortunately I'm working on an embedded device and stdbuf is not available – rico Jul 17 '14 at 02:48
0

Add a timeout to your read and if it times out, check whether MyApp is running, quit if not, read again if it is.

read -t 1 line
if [ $? -eq 1 ]; then
    # Timeout - check MyApp ruuning
    ps ... | grep MyApp ...
fi
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432