systemd `systemctl stop` aggressively kills subprocesses
Asked Answered
I

2

16

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.

Indecisive answered 30/11, 2016 at 20:58 Comment(1)
Have you reviewed the docs in man systemd.services and man systemd.kill ?Beamy
C
17

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.

Crater answered 5/12, 2016 at 10:2 Comment(1)
that's is opposite. the user.services default to mixed, which kills right away. what the question wanted was killMode=control-group but they were inheriting the bad default from his user.slice. That's why the timeout was being ignored. enjoy the longer explanation onCermet
G
3

A late (possible) answer, but as I googled for weeks with a similar issue, finding nothing, I figured I add my solution.
My error was that I ran the systemd unit as root and switched (using sudo) to "the correct" user in the startscript (inherited from SysVinit script).
That starts the processes in the user.slice which is killed mercilessly on shutdown. When I changed the unit file to run as the correct user (USER=myuser) and removed sudo from the start script, the processes start in the system.slice and get properly handled on shutdown.

Galop answered 21/10, 2019 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.