I am using the following to redirect output from my script to both terminal and log file while appending date to every line that goes to the log file:
NPIPE=/tmp/$$_$RANDOM.tmp
mknod $NPIPE p
tee <$NPIPE /dev/tty | ( while read line; do echo "$(date): ${line}"; done ) >> $LOG_FILE &
exec 1>&-
exec 1>$NPIPE
exec 2>&-
exec 2>$NPIPE Now in this same script i upload the log file to a server. The log file on the local device (embedded) looks complete, but the log file at the server is always partial.
I get the feeling that the pipe still is emptied in blocks and still holds content when i send the log, maybe i am missing some kind of a flush command but i can't find anything like that.
Any advice?
41 Answer
I believe that you’ll need to close the pipe:
exec 1>&-
exec 2>&-Then, after you upload the log file, you’ll need to start a new tee process:
tee <$NPIPE /dev/tty | ( while read line; do echo "$(date): ${line}"; done ) >> $LOG_FILE &
exec 1>$NPIPE
exec 2>$NPIPE 5