Killing systemd service with and without systemctl
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.
Related videos on Youtube
Gordon
Updated on September 18, 2022Comments
-
Gordon over 1 year
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 over 5 yearsI 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.
-
Michael Hampton over 5 years@Gordon SIGKILL is never safe.
-
Gordon over 5 yearsYeah we remove SIGKILL from the picture to be used but any other way with kill command and without systemctl kill?
-
Michael Hampton over 5 years@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.
-
Gordon over 5 yearsMy 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?
-
Michael Hampton over 5 years@Gordon Eh? "At any cost" and "safely" are mutually exclusive. What exactly do you really mean?
-
Gordon over 5 yearsI 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
-
Lucas Werkmeister over 5 yearsIf you use
systemctl stop
, the service will eventually be killed withSIGKILL
if it doesn’t exit properly. Seeman systemd.kill
for details.