I have a shell script that runs python.


# Setup environment
cd ~/foobar/ && \
    python3 -m venv env && \
    source env/bin/activate && \
    pip install -q -r requirements.txt && \
    find . -maxdepth 1 -type f -name "*.py" | xargs pylint

# Run scripts
python ~/foobar/main.py

If I ctrl+c in the terminal, it will shutdown the python properly. However if I pkill -9 start.sh, it will prompt Killed but the python is still running. Is there a way to kill the python script when the calling script dies?

  • I don't know if I'm more surprise by the -9 (is there a good reason for it?) or by the fact that start.sh really was in the process list (can you confirm it?)
    – Quasímodo
    Jun 4, 2020 at 10:57
  • You run all commands conditionally – except for the Python script which you run even if everything before crashes? ;-) Jun 4, 2020 at 10:59
  • why do you send -9? if you send -2 instead everything goes as if you typed ctrl+c from the terminal
    – LL3
    Jun 4, 2020 at 12:53

1 Answer 1


The interesting question is: What shall kill the Python script? If you annihilate the bash process with SIGKILL then it is certainly not going to do anything about the Python script.

You could start a background process which regularly checks for the main bash process still being there and kill the Python script in case it is gone.

If you can modify the Python script the it could regularly check whether its parent has gone (easiest: Check if parent PID is 1) and kill itself. That way accidentally killing the checker does not cause any problems...

The best solution may be outside both scripts. You could run the bash script as a (regular or transient, see man systemd-run) SystemD service and use systemctl for killing the script (i.e. killing the whole cgroup).

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.