How does systemd track a service's processes, even if they fork?
AdvancedConceptProcesses, Signals & systemd
Answer
Every service gets its own cgroup (/sys/fs/cgroup/system.slice/nginx.service/). All children stay in that cgroup no matter how many times they fork or daemonize, so systemctl stop can kill all of them (KillMode=control-group, the default). That is also how resource limits like MemoryMax= apply to the whole service.
systemd-cgls -u nginx.service
systemctl status nginx # the CGroup: tree
More on Processes, Signals & systemd
What is the difference between a program, a process and a thread?BeginnerHow are new processes created in Linux?IntermediateWhat is a zombie process, and how do you get rid of it?IntermediateWhat is an orphan process?BeginnerExplain the process states shown in ps (R, S, D, T, Z).IntermediateWhy can't you kill a process in D state?Advanced