If $PID_PARENT launched $PID_CHILD, how can I detach (disown
?) $PID_CHILD from $PID_PARENT so that, when I kill $PID_PARENT, $PID_CHILD keeps running like nothing happened?
Concretely, my $PID_PARENT is the process running Jenkins
(or the Java
process that runs the server that runs Jenkins) and $PID_CHILD is a reeeeeeally long job that I don't want to have to restart after restarting Jenkins (which is needed for some maintenance). Essentially, I want to stop Jenkins but not stop the long job it started and I know both PIDs.
UPDATE 1: I found out about disown
and tried from a login shell (so not the parent PID shell):
disown $PID_CHILD
but got
-bash: disown: 13924: no such job
The $PID_CHILD correct and doing
ps -o ppid= $PID_CHILD
returns $PID_PARENT
UPDATE 2: As per @Rui's answer, I made a temporary hack job in Jenkins that only runs, from the parent shell this time:
disown 13924
but still got
disown: 13924: no such job – amphibient
Best Answer
One way is to have the child process disassociate itself from the parent. This will require suitable code in the child process, or a wrapper script that performs the disassociation before executing the real code:
which if saved as
solitary
can be tested via something like:with the use of
logger(1)
because the standard filehandles were all closed as part of the disassociation. These may need to be redirected elsewhere for your application.Note that this may not be possible if Jenkins (or systemd or whatever) uses a PID namespace that the process cannot escape no matter how it forks itself, in which case you would need some container-level solution, or to revisit what you're trying to do.