1. Overview

In this tutorial, we’ll discuss how to create and remove systemd service files. systemd is one of the many Linux init systems available and currently is the one most used. It manages system processes and services.

2. Creating a systemd Service

systemd init uses various unit files to control different system and service operations. A unit file contains information about a process maintained by systemd.

The default location for unit files is the /lib/systemd/system directory. This includes the unit files that came with the OS as well as those that came with the programs we’ve installed.

For the custom unit files we’ve created, we keep them in the /etc/systemd/system directory. All the files in these directories have filename extensions that describe the type of unit file they are.

To make our work easier, sometimes, we need to create our systemd service files to manage some processes. For this demo, we’ll create a simple service that checks the system’s information every time we boot up or restart the computer. We’ll name our service file systeminfo.service. And before creating this service file, let’s write a small script (called system.sh) that checks the system information:

#A simple script to check computer information
echo "***This is the current computer information***" > mycomputer.txt
date >> mycomputer.txt
echo "********************" >> mycomputer.txt
uname -a >> mycomputer.txt
lscpu | grep 'Model name' >> mycomputer.txt
w >> mycomputer.txt
echo "**** Disk Info ****" >> mycomputer.txt
df | grep '/dev' >> mycomputer.txt
sleep 270
rm mycomputer.txt

We then save the script and make it executable:

$ chmod +x system.sh

Now, let’s create a new service. We’ll use the –force and –full options with the systemctl command:

$ sudo systemctl edit --force --full systeminfo.service

Next, let’s enter the contents of our service file:

[Unit]
Description=System information service
[Service]
Type=Simple
RestartSec=1
User=joe
ExecStart=/bin/bash /home/joe/system.sh
WorkingDirectory=/home/joe
[Install]
WantedBy=multi-user.target

The [Unit] section contains metadata of the unit and its relation to other units — for example, the service description. This is followed by [Service], which contains the configuration information that shows how to manage the service and find out its state. Lastly, we have the [Install] section, which defines the behavior of a unit after it’s enabled. The WantedBy directive specifies the relationship between this unit and other units.

We then reload the service files to include the systeminfo.service:

$ sudo systemctl daemon-reload

Now, let’s start our systeminfo.service file:

$ sudo systemctl start systeminfo.service

Let’s check its status to ensure it’s actively running:

$ sudo systemctl status systeminfo.service

If our service is active and running, we’ll see output on our console like:

systemd 1024x329

To enable our service file to run on startup/reboot, we run:

$ sudo systemctl enable systeminfo.service

3. Removing systemd Services

We can also remove systemd files when we no longer require them or when they conflict with other systemd unit files. Before removing any systemd file, we must be sure what kind of service it runs and what might be the outcome of deleting that particular file. We can also remove the modifications we’ve added to the default systemd service files.

3.1. Removing Custom systemd Services

First, we stop the service we want to remove. Let’s use the systeminfo.service we created earlier:

$ sudo systemctl stop systeminfo.service

Let’s make sure the service has stopped:

$ sudo systemctl status systeminfo.service

The service should be inactive/dead:

systemctlstop 1024x231
Next, let’s prevent the file from running after boot/reboot using:

$ sudo systemctl disable systeminfo.service

Now, let’s proceed to remove the file together with any symlink related to it:

$ rm /etc/systemd/system/*servicename
$ rm /usr/lib/systemd/system/*servicename 

Afterward, let’s reload the system files:

$ sudo systemctl daemon-reload

3.2. Removing Default/Installed systemd Services

We can also remove the systemd services that come preinstalled with the OS. Equally, we can also get rid of those that came with the software we’ve installed.

For the default services, we need to look for symlinks or copies of the same service files in other directories after removing those in the /etc/systemd/service directory. Before removing these services, we have to ensure we’ve stopped and disabled them. These are the other directories we look at:

$ rm /usr/local/lib/systemd/system/*servicename
$ rm /usr/local/etc/systemd/system/*servicename
$ rm /usr/lib/systemd/system/*servicename 
$ rm /etc/init.d/*servicename

3.3. Reverting a Service Definition to Vendor Default Settings

Sometimes, we may want to revert a service definition to vendor default settings without removing the service entirely.

In those cases, we’d run:

$ systemctl revert *servicename
$ sudo systemctl daemon-reload

This removes drop-in configurations that have modified the specified units as well as user-configured unit files that override the vendor defaults.

4. Conclusion

In this tutorial, we’ve looked at how to create a custom systemd service, how to remove it, and how to remove the default/installed systemd service. We’ve also looked at how to remove user configurations that override the vendor defaults using systemctl revert.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.