views:

323

answers:

2

I'm working on a Bash shell script that runs several Python scripts like so:

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

At one point, I killed the shell script (using kill PID), but the Python script continued running, even after the script terminated. I thought these would die as soon as the main script died. What am I misunderstanding about Bash scripting, and what can I do to get the functionality I'm looking for? Thanks in advance!

+1  A: 

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

a) The child process can ignore SIGHUP, or handle it a non-fatal manner.

b) The Child could disassociate itself from the parent process by fork() and becoming a process group leader.

You could just exec the python code, so that the shell is replaced with the python.

Douglas Leeder
I see, thanks! I think (a) is the problem in my case (probably a bare "except" clause).
jrdioko
+2  A: 

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

trap "echo killing childs; pkill -P $$"  EXIT
Jürgen Hötzel
It looks like that will do it, although I think you need to trap more than just EXIT.
jrdioko
And an `exit` at the end of the trap commands, so the script itself will die too.
jrdioko
And this won't even work... since the trap arguments won't be executed until after the current line terminates, and it's the current line that I want to terminate.
jrdioko
No, signal handlers are called async. An no need to call exit at the and of the trap commands, because a exit handler is called ON TERMINATION of the shell.
Jürgen Hötzel