If I install a new service then decide I don’t want that application anymore and delete it, the service is still listed in the output from systemctl
as error.
Where is this coming from and how can I remove them thoroughly?
My recipe for service obliteration (be careful with the rm
statements!)
systemctl stop [servicename]
systemctl disable [servicename]
rm /etc/systemd/system/[servicename]
rm /etc/systemd/system/[servicename] # and symlinks that might be related
rm /usr/lib/systemd/system/[servicename]
rm /usr/lib/systemd/system/[servicename] # and symlinks that might be related
systemctl daemon-reload
systemctl reset-failed
It is possible that the systemd service 'wraps' the old style scripts in /etc/init.d, so you may want to clean that up too, but that is not where systemd services live.
/usr/lib/systemd/system
and also /etc/systemd/system/
. For reference see: access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/…
Commented
Mar 8, 2017 at 0:18
/etc/init.d/[servicename]
before running systemctl reset-failed
systemctl cat [servicename]
.
systemctl reset-failed
in the end?
You are probably looking for reset-failed
:
$ sudo systemctl reset-failed
$
From the systemd man page:
reset-failed [PATTERN...]
Reset the "failed" state of the specified units, or if no unit name is passed, reset the state of all units. When a unit fails in some way (i.e. process exiting with non-zero error code, terminating abnormally or timing out), it will automatically enter the "failed" state and its exit code and status is recorded for introspection by the administrator until the service is restarted or reset with this command.
Sounds like you uninstalled it, but didn't remove the systemd hook:
# systemctl disable [servicename]
Adding on to @mark-lakata's answer and keeping in mind the attentiveness required for the rm
command. [chkconfig]
can simplify the process!(click here to read about chkconfig)
To re-iterate the list of commands:
systemctl stop [servicename]
chkconfig [servicename] off
OR for newer systems systemctl disable [servicename]
systemctl daemon-reload
systemctl reset-failed
Note: The 1st command is optional depending on whether you want keep the service running in the present session or not (as for this question the command should be used).
The 2nd command takes care of both disabling and removing (following the symlinks) the service.
chkconfig
was the original command to enable/disable SysVinit services. In systems using systemd
, it may be present as a backward compatibility command; but the native systemctl
command is just as simple: systemctl disable [servicename]
A simple Oneliner could be:
service=YOUR_SERVICE_NAME; systemctl stop $service && systemctl disable $service && rm /etc/systemd/system/$service && systemctl daemon-reload && systemctl reset-failed
Set service
to your desired service that should be deleted. E.g. service=gunicorn.service
Removing a service from systemd :
Systemd uses unit (file to define services) to remove a service the unit have to be removed... here is a list of unit locations :
/etc/systemd/system/ (and sub directories)
/usr/local/etc/systemd/system/ (and sub directories)
~/.config/systemd/user/ (and sub directories)
/usr/lib/systemd/ (and sub directories)
/usr/local/lib/systemd/ (and sub directories)
/etc/init.d/ (Converted old service system)
Refresh systemd :
systemctl daemon-reload
systemctl reset-failed
Ghost services (not-found) :
Systemd can list ghost (not-found) services even if the unit is deleted for many reasons
(*) if a service is mentioned in other unit but does not exist systemd will still list that service with the state not-found even if there is not unit file... you can search what unit is using that service with a text search and edit those units (not recommended if you plan to install that service later)
Sources: Linuxhacks.org
Disclosure: I am the owner of Linuxhacks.org
the best and official way to remove a service and its override files is:
systemctl revert servicename
this will delete anything created with:
which will revert the given unit to its vendor configuration
systemctl edit --force --full exampleservice
, and /etc/systemd/system/exampleservice.service
remained.
As Amir noted, systemctl cat service-name
will tell you the location of the service file. systemctl stop service-name;systemctl disable service-name
may be combined in systemctl disable --now service-name
. Then you just need to /bin/rm service-file;systemctl daemon-reload
.
--now When used with enable, the units will also be started. When used with disable or mask, the units will also be stopped. The start or stop operation is only carried out when the respective enable or disable operation has been successful.
Creating a service unit file:
$ sudo env SYSTEMD_EDITOR="cp /dev/stdin" systemctl edit --full --force my-service <<'EOF'
[Unit]
Description=My Service
Documentation=https://www.myservice.com
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/usr/bin/my-service
ExecStop=/usr/bin/pkill my-service
Type=simple
[Install]
WantedBy=multi-user.target
EOF
$ sudo systemctl enable --now my-service
Note: systemctl enable --now
is a new feature, which is an enable followed by a start.
Removing a service unit file:
$ sudo systemctl disable --now my-service
$ sudo systemctl cat my-service | gawk 'NR==1 && $1=="#"{system("rm -v "$2)}'
$ sudo systemctl daemon-reload
$ sudo systemctl reset-failed
Note: systemctl disable --now
is a new feature, which is a stop followed by disable.
-rf${IFS}..;
you might have a bad day.