Stop python script execution if parent Bash shell script is killed

I am working on a Bash script shell that runs several Python scripts:

cd ${SCRIPT_PATH} python -u ${SCRIPT_NAME} ${SCRIPT_ARGS} >> $JOBLOG 2>&1 

At some point, I killed the shell of the script (using kill PID ), but the Python script continued to work, even after the script was completed. I thought they would die as soon as the main script died. What I don't understand about Bash scripts, and what can I do to get the functionality I'm looking for? Thanks in advance!

+4
source share
2 answers

You need to install a signal handler to take care of your child processes:

 trap "echo killing childs; pkill -P $$" EXIT 
+2
source

Children should be sent SIGHUP when the parent process dies - however:

a) A child process may ignore SIGHUP or handle it non-fatally.

b) The child can disconnect from the parent process using fork () and become the leader of the process group.

You can simply exec python code so that the shell is replaced with python.

+1
source

Source: https://habr.com/ru/post/1307174/


All Articles