Home >Backend Development >Golang >How Can I Keep Child Processes Running After the Systemd-Managed Parent Process Exits?
Can't Detach Child Process when Main Process is Started from Systemd
When running a main process that spawns child processes, it's desirable to have the child processes survive even if the main process restarts or dies. This behavior works as expected when running the main process from a terminal, as demonstrated in the provided example with the exectest program.
However, when the main process is started via Systemd using a service file, the child processes do not survive beyond the lifetime of the main process. This happens because Systemd manages processes in a control group, and by default, it cleans up any child processes upon the parent process's termination.
To resolve this issue, the KillMode setting in the Systemd service definition can be modified. By default, KillMode is set to control-group, but changing it to process will ensure that only the main process is killed, allowing its child processes to continue running.
The updated service file would include the following line:
KillMode=process
With this setting in place, the child processes of exectest will survive even when the main process is killed or restarted via Systemd.
The above is the detailed content of How Can I Keep Child Processes Running After the Systemd-Managed Parent Process Exits?. For more information, please follow other related articles on the PHP Chinese website!