2

What's the difference between systemctl stop and systemctl kill?

systemctl kill supports sending a custom signal to the process, e.g. -9 (SIGKILL) instead of -15 (SIGTERM). So, is the only difference that systemctl kill is a more general version of systemctl stop, i.e. systemctl stop always sends -15 (SIGTERM)?

As an example, systemctl stop and systemctl kill have the same result for ssh.service (sshd):

systemctl stop:

$ systemctl stop sshd
$ systemctl status sshd
● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Mon 2021-11-08 09:09:32 CET; 1s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
  Process: 2086153 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
  Process: 2086154 ExecStart=/usr/sbin/sshd -D $SSHD_OPTS (code=exited, status=0/SUCCESS)
 Main PID: 2086154 (code=exited, status=0/SUCCESS)

systemctl kill:

$ systemctl kill sshd
$ systemctl status sshd
● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Mon 2021-11-08 09:10:15 CET; 1s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
  Process: 2086486 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
  Process: 2086487 ExecStart=/usr/sbin/sshd -D $SSHD_OPTS (code=exited, status=0/SUCCESS)
 Main PID: 2086487 (code=exited, status=0/SUCCESS)
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
Shuzheng
  • 4,023
  • 1
  • 31
  • 71
  • There are some additional details (albeit not _that_ detailed) under `man systemctl` that may help you find the answer to your question. – frippe Nov 08 '21 at 08:33

2 Answers2

11

systemctl kill is pretty similar to kill in that it simply sends a signal (default=SIGTERM). The main difference between kill and systemctl kill is you can specify a unit instead of a PID and systemd understands which processes you want to send that signal to.

On the otherhand systemctl stop will stop one or more units specified on the command line. First systemd will run any ExecStop= lines (if any). If any processes remain, handle these using KillMode= rules. After that, it sends SIGTERM (can be changed with KillSignal=). If SendSIGHUP= is set, it will send SIGHUP immediately after that. If the process doesn't handle SIGTERM well and fails to stop within 90s (can be changed with TimeoutStopSec=), then send SIGKILL (can be changed with FinalKillSignal=). In addition, if KillMode=control-group (default), then any child processes will also be killed.

So you can see that systemctl kill simply sends a signal. That may stop a unit, but systemd does not ensure that happens. Instead systemctl stop WILL stop a unit.


systemctl kill is equivalent to systemctl stop assuming:

  1. There is no ExecStop=
  2. KillSignal= in the unit file is equal to --signal=.
  3. The unit is guaranteed to respond to the first signal (no hanging) and does not need a followup.
  4. KillMode= is not set, or is equal to process.

One thing special about sshd.service is it contains:

ExecReload=/usr/sbin/sshd -t
ExecReload=/bin/kill -HUP $MAINPID

In this case, you may ask: What's the different between these two commands:

# systemctl reload sshd
# systemctl kill --signal=SIGHUP --kill-who=sshd.service

While they are almost the same, the difference is quite obvious here... reload calls /usr/sbin/sshd -t before the signal.


Another special thing about sshd.service is it contains KillMode=process. This makes the systemctl kill and systemctl stop a little closer as orphaned child processes will not be cleaned up by systemd. By default KillMode=control-group will cause systemd to clean up all orphaned child processes

Stewart
  • 12,628
  • 1
  • 37
  • 80
1

According to the manual, kill:

kill PATTERN...
Send a signal to one or more processes of the unit. Use --kill-who= to select which process to kill. Use --signal= to select the signal to send.

And stop:

stop PATTERN...
Stop (deactivate) one or more units specified on the command line.

So kill will kill processes of a spefic unit, while stop will stop specified units.

You can see the difference here:

# theres's no process killed
$ sudo systemctl kill ufw
$ sudo systemctl status ufw
● ufw.service - Uncomplicated firewall
     Loaded: loaded (/lib/systemd/system/ufw.service; enabled; vendor preset: enabled)
     Active: active (exited) since Mon 2021-11-08 05:47:44 -03; 18s ago
       Docs: man:ufw(8)
    Process: 48409 ExecStart=/lib/ufw/ufw-init start quiet (code=exited, status=0/SUCCESS)
   Main PID: 48409 (code=exited, status=0/SUCCESS)

$ sudo ufw status
Status: active

Hasta                      Acción      Desde
-----                      ------      -----
80                         ALLOW       Anywhere                  
...           

$ sudo systemctl restart ufw
$ sudo systemctl stop ufw
$ sudo systemctl status ufw
● ufw.service - Uncomplicated firewall
     Loaded: loaded (/lib/systemd/system/ufw.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Mon 2021-11-08 05:49:08 -03; 1s ago
       Docs: man:ufw(8)
    Process: 48785 ExecStart=/lib/ufw/ufw-init start quiet (code=exited, status=0/SUCCESS)
    Process: 48995 ExecStop=/lib/ufw/ufw-init stop (code=exited, status=0/SUCCESS)
   Main PID: 48785 (code=exited, status=0/SUCCESS)
$ sudo ufw status
Status: inactive
schrodingerscatcuriosity
  • 12,087
  • 3
  • 29
  • 57