1

I want to run two commands simultaneously in bash script (one of them is another bash script) and I need both of them to stop when I press Ctrl+C. My bash script now is:

#!/bin/bash

./command1 &

exec "/path/to/bash2" 

and in bash2:

#!/bin/bash

[..]
python run.py

Should I add a trap to kill both commands? If so, adding the following in bash did not help

trap killgroup SIGINT

killgroup(){
  echo killing...
  kill 0
}
1

Try this to ensure you're kill-ing the correct PID:

./command1 &
childpid=$!
trap "kill -TERM ${childpid}" EXIT
0

GNU Parallel respects ctrl-c. So you can run:

parallel ::: ./command1 /path/to/bash2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.