12

What is the difference between killing an running daemon systemd service like this :

kill -SIGKILL 3645

and

systemctl -s kill -SIGKILL 3645

where 3645 is the pid of the systemd service.Also are there any drawbacks of using the first method?

Gordon
  • 141
  • 1
  • 1
  • 4

1 Answers1

27

First, your systemctl syntax is wrong: systemctl kill expects a unit name, not a PID, and -s requires a signal name. As written, it won't do anything but give you an error message:

Failed to parse signal string kill.

If you fix that, it will then tell you:

Failed to kill unit 3645.service: Unit 3645.service not loaded.

The correct syntax would be:

systemctl kill -s SIGWHATEVER whatever.service

The difference, obviously, is that you can send signals to processes based on the systemd unit, rather than pid. Because a unit might run multiple processes, systemd can then send the same signal to all processes running under the unit, and indeed, this is the default. The --kill-who= option lets you change this if necessary.

Generally, though, it's not a good idea to get into the habit of sending SIGKILL to anything. You should stop your services normally whenever possible, e.g. with systemctl stop whatever.service. SIGKILL is the same as the potentially destructive kill -9 you have probably heard of and been warned about. And, if you kill a service via either method and it is configured to restart automatically, it may do so. This may or may not be what you want, depending on why you are trying to send a kill signal.

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
  • I haven't set the service to get restarted or in other words Restart=no , also the service is Type=forking and it is set to be "process".Since it is a forking process it has 1 child ( it doesn't spawn more) and parent can it be killed safely with the kill command and any params(for example kill -0 3645). I am asking this question from curiosity. – Gordon Oct 17 '18 at 20:17
  • @Gordon SIGKILL is never safe. – Michael Hampton Oct 17 '18 at 20:18
  • Yeah we remove SIGKILL from the picture to be used but any other way with kill command and without systemctl kill? – Gordon Oct 17 '18 at 20:18
  • @Gordon I can't think of any reason to send a kill signal to a process or unit in normal operation. You're not likely to ever have a reason to do so. – Michael Hampton Oct 17 '18 at 20:21
  • My reason is to stop the service at any cost which means safely .I am not sure if systemctl stop or systemctl kill will do it at any cost that is why I started searching for ways with the kill command,is it doable safely and all the time working without systemctl stop or systemctl kill? – Gordon Oct 17 '18 at 20:30
  • 2
    @Gordon Eh? "At any cost" and "safely" are mutually exclusive. What exactly do you really mean? – Michael Hampton Oct 17 '18 at 20:34
  • I mean that if I want to stop the service I will stop it for sure,not get an exception for example and the service to continue to run – Gordon Oct 17 '18 at 21:07
  • 1
    If you use `systemctl stop`, the service will eventually be killed with `SIGKILL` if it doesn’t exit properly. See `man systemd.kill` for details. – Lucas Werkmeister Oct 17 '18 at 21:22