0

I have a script that looks like this:

#!/bin/bash

trap 'kill $(jobs -pr) && exit' SIGINT

# Run script 10 times in the background
for i in `seq 1 10`;
do
    python script.py &
    sleep .05
done
# Wait for result from program
sleep 5

# Cleanup scripts
kill $(jobs -p)

The python script is basically a stress test for a different program, and I want 10 of them running at the same time. They stay that way for 5 seconds so I can observe what happens and then they all get killed.

The problem is, if I CTRL+C out of running the script before it's complete, the background python jobs hang around and keep going. I want them all to exit when this script exits, whether it exits normally or is killed. I searched around but didn't find much.

I have tried the solutions posted in this answer, but at least for me, $(jobs -p) returns no processes when called from the trap so none of those solutions work. Edited to include trap code.

Community
  • 1
  • 1
bobroxsox
  • 902
  • 2
  • 10
  • 26

0 Answers0