1

I have a Python script (test.py) like the following:

try:
    while True:
        print("test: {}".format(argv[1]))
    
        time.sleep(1)
except KeyboardInterrupt:
    print("process terminated!")

So I can stop the program by typing Ctrl-c.

Now I need to execute multiple test.py instances with different arguments. So I am using a Bash script to run multiple Python invocations in parallel like the following:

#!/bin/bash

export PYENV="/home/AA/.venv/env1/bin/python3"

cd /home/AA/test
sudo $PYENV test.py 1 &
sudo $PYENV test.py 2 &

wait

The question is: when I want to terminate the processes (including the Python processes) by typing "ctrl+C", it's not working.

How can I terminate these background processes by typing "ctrl+C"?

A.Yang
  • 21
  • 2

1 Answers1

0

This does not activate the exception when pressing CTRL-C, but maybe it is good enough:

export PYENV="/home/AA/.venv/env1/bin/python3"

cd /home/AA/test
doit() { sudo $PYENV test.py "$1"; }
export -f doit

parallel -u doit ::: 1 2
Ole Tange
  • 31,768
  • 5
  • 86
  • 104