5

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!

2 Answers 2

3

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

trap "echo killing childs; pkill -P $$"  EXIT
Sign up to request clarification or add additional context in comments.

4 Comments

It looks like that will do it, although I think you need to trap more than just EXIT.
And an exit at the end of the trap commands, so the script itself will die too.
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.
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.
1

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.

1 Comment

I see, thanks! I think (a) is the problem in my case (probably a bare "except" clause).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.