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:


./command1 &

exec "/path/to/bash2" 

and in bash2:


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

  echo killing...
  kill 0

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

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

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.