529

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?

0

9 Answers 9

838

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.

13
  • 18
    Be aware that there are multiple locations where Systemd unit files are stored, notably /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
  • 20
    I had also to remove /etc/init.d/[servicename] before running systemctl reset-failed
    – Andrea
    Commented Jun 7, 2017 at 15:21
  • 19
    Right, I forgot to disable before removing the unit files. BTW, to find all unit files to remove, I inspect the output of systemctl cat [servicename].
    – Amir
    Commented Sep 17, 2017 at 14:55
  • 2
    Why doesn't systemd provide a command to cleanup? Systemd knows how to cleanup after deleted *service and *.timer files. Why does systemd think users know the inner workings of the program???
    – jww
    Commented Jun 19, 2019 at 2:07
  • 10
    @MarkLakata Why do we need to do systemctl reset-failed in the end?
    – tuk
    Commented May 19, 2020 at 4:14
98

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.

4
  • 23
    That isn't what the question is asking for at all. Why on earth has this been upvoted 17 times?
    – psusi
    Commented Nov 7, 2018 at 16:54
  • 2
    It sounds like this just resets the state for a service that should no longer exist. You should very likely delete the service files themselves, not simply change the state of a service you no longer want.
    – pzkpfw
    Commented May 19, 2022 at 8:30
  • There are no service files to delete if it is a transient unit. Commented Dec 17, 2022 at 17:44
  • It worked nicely for a service instance. Now the instance isn't listed anymore.
    – U. Windl
    Commented Jul 1 at 10:46
44

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

# systemctl disable [servicename]

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.

4
  • 3
    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]
    – telcoM
    Commented Jul 29, 2018 at 13:29
  • 3
    Okay, but the reason for me using this command is, you then don't have to explicitly run the rm command Commented Jul 29, 2018 at 19:23
  • 1
    chkconfig not found in ubuntu 16
    – Nam G VU
    Commented Nov 14, 2019 at 10:26
  • 3
    systemctl disable is not removing the unit files for me
    – afarley
    Commented Feb 6, 2021 at 19:22
8

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

0
6

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

4

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

1
  • This almost deletes everything; I had created a service with systemctl edit --force --full exampleservice, and /etc/systemd/system/exampleservice.service remained.
    – PFudd
    Commented Nov 23, 2023 at 2:37
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.

2
  • the awk(1) approach is genius
    – sjas
    Commented Aug 30 at 7:42
  • Thank you! I'm still not completely happy with the awk command, because it doesn't escape $2, and for security it only deletes the file mentioned on the first line (so not override files). If a bad actor names the service file -rf${IFS}..; you might have a bad day.
    – PFudd
    Commented Sep 3 at 18:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .