Search code examples
bashmonitoringbenchmarkingsleep

How to run two commands, but with a delay on the second command without stopping the first command?


I'm trying to do the following (on a Raspberry Pi 3 with Raspbian OS):

  1. start a benchmark (sysbench)
  2. then run some monitoring tools (iostat and mpstat) after some delay, say 5s, as a warm-up interval

So I made the following base script:

#!/bin/bash
for x in 16000 32000 64000 128000
do
  echo "max-prime = $x"
  (sysbench --test=cpu --cpu-max-prime=$x --num-threads=4 run >> results.out) & (sleep 5s && mpstat >> mpstat.out & iostat >> iostat.out)
done

I tried some more variations of the 5th line above but sysbench was not being executed properly (I think because of sleep?). The output written in results.out is only this, repeated 4 times because of the loop:

sysbench 0.4.12: multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 4

Doing CPU performance benchmark

Threads started!

How do I execute sysbench and run the monitoring tools after 5 seconds, without compromising sysbench?


Solution

  • You'll have an easier time of it if you put the commands on separate lines.

    for x in 16000 32000 64000 128000
    do
      echo "num of threads = $x"
      sysbench --test=cpu --cpu-max-prime=$x --num-threads=4 run >> results.out &
      sleep 5s
      mpstat >> mpstat.out
      iostat >> iostat.out
    done
    

    You'll want to wait until the benchmark finishes before going to the next loop. I recommend putting either wait or kill %% at the end of the loop to either wait for or stop it.