From this answer we have learned that you can implement reliable killing of entire process subtrees with Linux PID namespaces via
Here is problem with it that I don't understand:
It only works when I use the
--forkoption to unshare.
unshare -fp -- bash -c "watch /bin/sleep 10000 && echo hi"
When I run this, and
kill -9the PID of that
bash, then watch, sleep etc are all dead, as I desire.
But when I use it without
unshare -p -- bash -c "watch /bin/sleep 10000 && echo hi"
kill -9the bash PID, then the
watchgets reparented to PID 1 (on my Ubuntu that's systemd), so I don't have the desired effect of killing all children.
- Why is
--forknecessary to get the desired effect? Why is
exec()without fork not enough?
- Is there a workaround for this? I would prefer if I could conveniently send
kill -9to the PID created by starting
unshareto kill everything below it. But when I use
--fork, killing the pid returned when I started
unsharewill, well, simply kill
bashreparented to PID 1, because
unshareis not in my PID namespace.
&& echo hi is needed because if you give only one command to
bash -c, it will
exec() it and thus the bash process is gone (replaced) and you can't kill its PID.