0

I am trying to record capacity utilization while running other (Python) processes with the following script:

#!/bin/bash

rm results/*

top -U $USER > results/test_utilization.txt &

# Give `top` enough time to record something
python3 -c 'import time;time.sleep(10)'

echo "JOB_1_COMPLETE" >> results/test_utilization.txt

python3 -c 'import time;time.sleep(10)'

echo "JOB_2_COMPLETE" >> results/test_utilization.txt

kill %1

My hope is that running something like echo "JOB_<N>_COMPLETE" >> results/test_utilization.txt will enable me to more easily associate metrics from top with each "job". However, when I do this: cat results/test_utilization.txt | grep JOB all I get is this:

$ cat results/test_utilization.txt | grep JOB
JOB_2_COMPLETE

It seems test_utilization.txt is being overwritten, or something. How can I fix this?

blacksite
  • 12,086
  • 10
  • 64
  • 109
  • Use `>>` instead of `>` to append – that other guy Nov 29 '18 at 21:03
  • Simple enough -- thanks! – blacksite Nov 29 '18 at 21:07
  • 1
    I marked it as a dupe, but it deserves a bit more info: when redirecting with `>`, every background write will continue where the previous write to the same fd left off. Any text appended in the mean time will be bulldozed over. With `>>`, every new write will append to the current end of the file, after any other data that other processes have appended. – that other guy Nov 29 '18 at 21:15

0 Answers0