Search code examples
forksystemd

How systemd tracks fork process with Type=fork


I am interested how systemd tracks child of main process that remains after main process exists?


Solution

  • Systemd is using cgroups for this. Before starting any executable that is defined with a service file, systemd creates a cgroup and puts the executable in it[*]. After that, it doesn't matter how many child processes or how many times the processes in the cgroup have forked. For systemd, they all belong to the same cgroup.

    Another good thing to know is that when the parent process dies, child processes of that parent process inherits PID 1 (systemd) as their parent process. This is a unix rule.

    [] System first creates a cgroup. Then forks it self and puts the new process in this cgroup. Then calls exec() family calls for your service's ExecStart= binary.