Daemonized with double-fork process became defunct

Hello everyone. I’m trying to spawn a daemon process inside a pod using the two-fork and setsid technique.

This scenario works well when I do it in a regular Linux environment: there are no defunct processes at any step. However, when I try to do this in a Kubernetes pod, I get a defunct record (of the grandchild process) when the grandchild process exits.

I think the reason for this is that my app process is an init process with PID 1, and the new daemonized process is still its child in the pod (which is not the case in the regular Linux environment).

Does anyone have any ideas on how to daemonize a process in a Kubernetes pod without creating zombie processes?

You need something to remain PID1, and that something must wait() on child processes

In the non-containerized linux environment that will be something like systemd. The processes with no parent to wait on them get re-parented to PID1 there as well and well-behaved init systems detect this and clear these entries.

YMMV for which init works best for you in the container.

There are some dedicated projects like GitHub - krallin/tini: A tiny but valid `init` for containers

Some shells can do this, though note that you need to ensure the shell remains running as PID1 and is not exec to the child process.

This is a good writeup IMHO Docker and the PID 1 zombie reaping problem