Shell Script: Run benchmark and cpustat together

Hello,
New to Shell scripting here. :frowning:

I am trying to do the following:

  • Run a long benchmark program (Don't need to see the outputs to stdout)
  • As long as the benchmark is running, run the "cpustat" command with one particular Performance Counter.
  • When the (Multi-thread) benchmark is done running, the cpustat command should also stop.

So far I have been trying different things like:

cpustat -c pic0=SB_full,pic1=Instr_cnt 1
./long_multithread_benchmark &

kill -9 `pgrep cpustat`

Also, tried using the "while" loop by checking for the $? of the last command:

./long_benchmark

while [[ $? -neq 0] ]
do
cpustat -c pic0=SB_full,pic1=Instr_cnt 1
done

None of them are working.

Thanks is advance for any suggestions/advice.

What you need is the special variable $! which is the process ID of the last thing you put in the background. You can feed that into kill to get it to kill the right program.

Don't use -9 unless you absolutely have to. It will probably respond to less severe signals and make less mess by doing so.

cpustat ... &
PID="$!"

./long-multithread-benchmark

kill -TERM "$PID"
wait