Home >Backend Development >Golang >How to Ensure Child Processes Survive Parent Process Termination in systemd?

How to Ensure Child Processes Survive Parent Process Termination in systemd?

Susan Sarandon
Susan SarandonOriginal
2024-12-25 08:24:10608browse

How to Ensure Child Processes Survive Parent Process Termination in systemd?

Detaching Child Processes in Systemd

When executing a program that spawns long-running child processes, it is crucial to ensure that the child processes remain active even after the main process restarts or terminates. Using the command line, this can be achieved by setting the Setsid flag of the exec.Command object to true. This detaches the child process from the parent process's session and process group, allowing it to continue running independently.

However, when launching the main process through systemd, the default KillMode setting in the service configuration file may interfere with the detachment process. By default, systemd's KillMode is set to control-group, which implies that all processes within the unit's control group are terminated when the main process stops.

To resolve this issue and allow the child processes to survive the main process termination, the KillMode setting in the systemd service file needs to be modified.

Modifying Systemd Service File

To change the KillMode setting, edit the systemd service file located at /etc/systemd/system/exectest.service and add the following line within the [Service] section:

KillMode=process

Setting KillMode to process ensures that only the main process itself is killed when the unit is stopped. This ensures that the child processes remain unaffected and continue running independently.

After making the change, save the file and restart the systemd service using the following commands:

sudo systemctl daemon-reload
sudo systemctl restart exectest

Verifying Child Process Survival

To verify that the child processes are surviving the parent process termination, use the following steps:

  1. Check the running processes using ps -ef | grep exectest: This should show the main process (exectest) running under the systemd user and the child process (exectest child) still active.
  2. Kill the parent process: Send a signal to the main process (e.g., sudo kill -INT ) to terminate it.
  3. Verify child process survival: Re-run ps -ef | grep exectest to check if the child process is still running. If the child process remains active, then the modification to the service file was successful.

The above is the detailed content of How to Ensure Child Processes Survive Parent Process Termination in systemd?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn