Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

systemd `systemctl stop` aggressively kills subprocesses

I've a daemon-like process that starts two subprocesses (and one of the subprocesses starts ~10 others). When I systemctl stop my process the child subprocesses appear to be 'aggressively' killed by systemctl - which doesn't give my process a chance to clean up.

How do I get systemctl stop to quit the aggressive kill and thus to allow my process to orchestrate an orderly clean up?

I tried timeoutSec=30 to no avail.

like image 276
GoZoner Avatar asked Nov 30 '16 20:11

GoZoner


People also ask

How does systemd stop a process?

4 Identifying and stopping processes in systemd services The systemd-cgls command displays all processes that belong to a systemd service, and systemctl SIGNAL PROCESS stops them.

What is KillMode in systemd?

KillMode= Specifies how processes of this unit shall be killed. One of control-group , mixed , process , 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= ).


1 Answers

KillMode= defaults to control-group. That means every process of your service is killed with SIGTERM.

You have two options:

  • Handle SIGTERM in each of your processes and shutdown within TimeoutStopSec (which defaults to 90 seconds)
  • If you really want to delegate the shutdown from your main process, set KillMode=mixed. SIGTERM will be sent to the main process only. Then again shutdown within TimeoutStopSec. If you do not shutdown within TimeoutStopSec, systemd will send SIGKILL to all your processes.

Note: I suggest to use KillMode=mixed in option 2 instead of KillMode=process, as the latter would send the final SIGKILL only to your main process, which means your sub-processes would not be killed if they've locked up.

like image 61
Benjamin Franzke Avatar answered Oct 04 '22 22:10

Benjamin Franzke