556

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?

Synetech
  • 69,547
eMeL
  • 5,661

9 Answers9

875

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.

Mike
  • 103
Mark Lakata
  • 9,588
107

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.

44

Sounds like you uninstalled it, but didn't remove the systemd hook:

# systemctl disable [servicename]

nerdwaller
  • 18,014
18

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:

  1. systemctl stop [servicename]
  2. chkconfig [servicename] off OR for newer systems systemctl disable [servicename]
  3. systemctl daemon-reload
  4. 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.

10

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

Leonleon1
  • 101
  • 1
  • 3
7

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

  1. unit still present on one of the systemd directory
  2. unit does not exit but a file link is still present on one of the systemd directory
  3. the service is used in other unit(s)*

(*) 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

intika
  • 1,383
5

the best and official way to remove a service and its override files is:

systemctl revert servicename

this will delete anything created with:

  • systemctl edit
  • systemctl set-property
  • systemctl mask

which will revert the given unit to its vendor configuration

2

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.

1

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.

PFudd
  • 111