I'd like to set the parent of a newly started process, is that possible?
Example, let us assume that we start a new desktop environment session via a login manager, so our process tree would look something like this:
init
\- login-manager
\- de-session
Now I do have a script to launch my most essential applications which should start with the session, for various reasons I'd like to keep these as a script and not migrate them to the autostart manager of any DE. It looks something like this:
#!/usr/bin/env
application1 &
application2 &
application3 &
After running this automatically at the start of the session, our process tree looks like this:
init
|- application1
|- application2
|- application3
\- login-manager
\- de-session
But what I'd actually like is to "reparent" these processes under the session, like this:
init
\- login-manager
\- de-session
|- application1
|- application2
\- application3
So, is there any way to "reparent" a process under another one?
Best Answer
On a few systems, you can mark a process as a child subreaper, which makes it take
init
's role of adopting orphan processes for all its descendants.On Linux, that's done with the
PR_SET_CHILD_SUBREAPER
prctl()
.So, you could start your
de-session
as (here hardcoding the value ofPR_SET_CHILD_SUBREAPER
for Linux):But
de-session
might get confused when it receives SIGCHLD signals for processes it has never spawned. Yourinit
has been designed to deal with those, but probably not yourde-session
, so you may find that you get an army of zombies asde-session
never acknowledges the death of those processes it never wanted to inherit.