Home >Backend Development >Golang >How Can I Keep Child Processes Running After a Systemd Service Stops?
Systemd: Detaching Child Processes
When starting a long-running process from the terminal, you may desire child processes to persist even if the main process restarts or terminates. While this is feasible with manual execution, it becomes problematic when running the main process as a systemd service.
In systemd, the default child process termination method is through control-group, which results in all child processes being terminated upon the parent process's termination. To prevent this, you can specify KillMode=process in the service configuration.
Here's how to implement it for the example given:
[Unit] Description=ExecTest [Service] Type=simple ExecStart=/home/snowm/src/exectest/exectest User=snowm KillMode=process [Install] WantedBy=multi-user.target
By setting KillMode=process, you instruct systemd to terminate only the main process when ending the service. This allows child processes to continue running even after the parent process has been stopped.
Please note that this approach only affects child processes that are created during the main process's execution. Processes created beforehand will still be terminated when the main process ends.
The above is the detailed content of How Can I Keep Child Processes Running After a Systemd Service Stops?. For more information, please follow other related articles on the PHP Chinese website!