When spawning child processes from a main process, it's crucial to ensure that the child processes remain active even when the main process is terminated. This behavior is particularly desirable when managing long-running background tasks.
In your specific scenario, you want to detach child processes from the main process (exectest) when it's started from the systemd service manager. However, you've observed that the child processes terminate along with the main process.
The solution lies in modifying the systemd configuration file (/etc/systemd/system/exectest.service) to include the following line:
KillMode=process
By default, systemd applies a "control-group" killing mode, which terminates all child processes within the main process's control group upon termination. By changing it to "process," you instruct systemd to only kill the main process itself, leaving the child processes untouched.
This modification ensures that the child processes continue to execute and survive the termination or restart of the main process, even when it's started from systemd.
The above is the detailed content of How to Prevent Systemd from Killing Child Processes When the Main Process Exits?. For more information, please follow other related articles on the PHP Chinese website!