Can't detach child process when main process is started from systemd

后端 未结 2 671
谎友^
谎友^ 2020-12-01 14:56

I want to spawn long-running child processes that survive when the main process restarts/dies. This works fine when running from the terminal:

$ cat exectest         


        
相关标签:
2条回答
  • 2020-12-01 15:16

    Solution is to add

    KillMode=process
    

    to the service block. Default value is control-group which means systemd cleans up any child processes.

    From man systemd.kill

    KillMode= Specifies how processes of this unit shall be killed. One of control-group, process, mixed, none.

    If set to control-group, all remaining processes in the control group of this unit will be killed on unit stop (for services: after the stop command is executed, as configured with ExecStop=). If set to process, only the main process itself is killed. If set to mixed, the SIGTERM signal (see below) is sent to the main process while the subsequent SIGKILL signal (see below) is sent to all remaining processes of the unit's control group. If set to none, no process is killed. In this case, only the stop command will be executed on unit stop, but no process be killed otherwise. Processes remaining alive after stop are left in their control group and the control group continues to exist after stop unless it is empty.

    0 讨论(0)
  • 2020-12-01 15:21

    If you cannot (like me) to change the KillMode of the service for some reason, you could try the at command (see man).

    You can schedule your command to run 1 minute ahead. See an example:

    # this will remove all .tmp files from "/path/" in 1 minute ahead (this task will run once)
    echo rm /path/*.tmp | at now + 1 minute
    
    0 讨论(0)
提交回复
热议问题