What would cause a SIGTERM to not propagate to child processes?
Asked Answered
E

2

9

I have a process on Linux that starts up 20 child processes via fork. When I kill the parent process, it will often kill all of the child processes, but sometimes it doesn't kill all of them, and I'm left with some orphaned processes. This isn't a race condition on startup, this is after the processes have been active for several minutes.

What sort of things could cause SIGTERM to not propagate to some child processes properly?

Estimative answered 29/12, 2015 at 20:4 Comment(1)
Thank you. This clears up a lot of confusion I've been having, I thought child processes were automatically killed for some reason and I was getting confused.Estimative
S
10

There is no automatic propagation of signals (SIGTERM or otherwise) to children in the process tree.

Inasmuch as killing a parent process can be observed to cause some children to exit, this is due to ancillary effects -- such as SIGPIPEs being caused when the child attempts to read or write to a pipeline with the dead parent on the other side.

If you want to ensure that children are cleaned up when your process receives a SIGTERM, install a signal handler and do it yourself.

Scalise answered 29/12, 2015 at 22:54 Comment(1)
By the way, the kernel sends SIGKILL and SIGINT to all processes with controlling terminal as the one that sends the signal. Also, signals sent to a group of processes get sent to several processes (SIGHUP is sent to the process group of the session owner)Tarmac
A
4

If you use process group id (pgid) when sending a signal, the signal would be propagated to parent process and all its children.

To know pgid, use ps a -o pgid,command.

Affection answered 25/11, 2016 at 11:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.