Live data from Hacker News

Killing a process and all of its descendants

morningcoffee.io

1–10 of 76 posts

Re: Killing a process and all of its descendants

#3
This is why cgroups were invented. They solve this problem. Start a process in its own cgroup, and you can later confidently kill the process and all of its descendants. Container "technologies" use cgroups extensively, as does systemd service management.

Re: Killing a process and all of its descendants

#6
post #4
post #2

Even waiting for a process to exit is surprisingly hard (impossible?) in Linux, unless it's your child.

wait [-fn] [jobspec or pid …] https://www.gnu.org/software/bash/manual/html_node/Job-Contr...

"Wait until the child process..."

So, no. That's not it.

Re: Killing a process and all of its descendants

#7
post #5
post #2

Even waiting for a process to exit is surprisingly hard (impossible?) in Linux, unless it's your child.

Not anymore: b53b0b9d9a613c418057f6cb921c2f40a6f78c24 (pidfd: add polling support)

Wow, finally! Committed just 3 months ago. Might be able to finally rely on it in Linux distros a few years from now!

https://github.com/torvalds/linux/commit/b53b0b9d9a613c41805...

Re: Killing a process and all of its descendants

#9
post #3

This is why cgroups were invented. They solve this problem. Start a process in its own cgroup, and you can later confidently kill the process and all of its descendants. Container "technologies" use cgroups extensively, as does systemd service management.

That's definitely the correct way to do this today. But even then `kill -9 $(< /sys/fs/cgroup/systemd/tasks)` is not enough if your goal is to reliably kill all processes because that's not atomic. Instead you'll have to freeze all processes, send SIGKILL and then unfreeze.

Re: Killing a process and all of its descendants

#10
post #8
post #2

Even waiting for a process to exit is surprisingly hard (impossible?) in Linux, unless it's your child.

That is getting considerably easier with the addition of pidfds, though: https://lwn.net/SubscriberLink/794707/93ffb35438fd3710/

Posted 1 week ago! Do you know what took them so long to realize this is the correct way to do it? Windows has had it for some two decades...
Post reply on HN