Capturing the stdout/err of nonterminating binaries

Its work again :)

Came across this issue at work. I did not have any control over the binary, otherwise, could have done the logging from inside the binary.

Anyways, it goes something like this. I wanted to capture std output and error of a few binaries which run forever. The first option of-course is O/P redirection -
$PATH/foobin >> $PATH/foolog 2>&1 &

But what happens to file size if binary does not terminate for months ? :S

I tried clearing the content of file once it reaches 10MB, but no luck, because while clearing the content, the file is still in use and so, as soon as the log is written again, file size is retained, even if half of the file is empty.


var=`ls -l $PATH/foolog | awk '{print $columnno}'`
if [ $var -gt 10000000 ]
then
>foolog
fi


This does not help :(

So the solution is something like this.

Do not redirect the O/P, instead pipe it to a script and let the script do the job.
$PATH/foobin 2>&1 | $PATH/foo.sh &

foo.sh -

while true
do
read line
if [ ! -z "$line" ]
then
echo $line >> $PATH/foolog
fi
var=`ls -l $PATH | grep foolog | awk '{print $columnno}'`
if [ $var -gt 10000000 ]
then
rm $PATH/foolog
fi
done


It's done.

0 comments:

Post a Comment