Search code examples
bashtee

tee won't write to file until script finishes executing?


I'm trying to automatically collect performance data (loading and screen rendering times) from an Android app. I use test-runner.py (an internal tool) to build the app and run the tests before using the adb command to get the performance data. My bash script looks like this:

BASE_DIR=../Application/app/src/custom-feeds
CTF_DIR=../../../../ComponentTestFramework
OUTPUT_FILE=out.txt

if test -e $OUTPUT_FILE
then
    rm $OUTPUT_FILE
fi

teardown ()
{
    cd $BASE_DIR
    adb logcat -d PerformanceTest:D *:S | grep 'Loading' | tee -a $OUTPUT_FILE
    adb logcat -d ActivityManager:I *:S | grep 'ContentBrowseActivity:\s+\+' | tee -a $OUTPUT_FILE
    ./restore.sh
}

# small sample feed

echo Testing sample feed with 10 initial items
echo size = 10 > $OUTPUT_FILE
cd $CTF_DIR
python3 test_runner.py -f $BASE_DIR/config-B-10.json
teardown

# medium sample feed

echo Testing sample feed with 100 initial items
echo size = 100 > $OUTPUT_FILE
cd $CTF_DIR
python3 test_runner.py -f $BASE_DIR/config-B-100.json
teardown

# large sample feed

echo Testing sample feed with 500 initial items
echo size = 500 > $OUTPUT_FILE
cd $CTF_DIR
python3 test_runner.py -f $BASE_DIR/config-B-500.json
teardown

# super large sample feed

echo Testing sample feed with 5000 initial items
echo size = 5000 > $OUTPUT_FILE
cd $CTF_DIR
python3 test_runner.py -f $BASE_DIR/config-B-5000.json
teardown

When the script is first run, the data gets written to the output file as expected:

size = 100

However, nothing else gets written until the script finishes executing. The output file now looks like this:

size = 5000
D/PerformanceTest( 1458): Loading time: 332874 ms
I/ActivityManager( 1843): Displayed com.amazon.android.calypso/com.amazon.android.tv.tenfoot.ui.activities.ContentBrowseActivity: +563ms

There are two issues:

  1. The original data ("size = 100") has been written. This shouldn't happen because of the -a switch in tee.
  2. The intermediate results aren't being written to the file even though the command works when I execute it outside the script.

What am I doing wrong?


Solution

  • Answering my own question in embarrassment.

    The issue was due to my use of > (which overwrites the file) than >> (which appends to it). Using >> obviously solved the problem. I'm still facepalming as I type this.