Linux

Linux: How to use Systemd to list services

Mainstream Linux distributions all use Systemd, an init system that works based on enabling and disabling “service” files. If you’re trying to list these service files but don’t know how to do it, you’ll need to make use of the systemctl command.

In this guide, we’ll go over the various way you can list Systemd service files on Linux. To get started, ensure you’re using a Linux OS with Systemd. Then, open up a terminal window and follow along.

Use Systemd to list active services

If you’re using Systemd a lot, at some point, you may want to know what services are active while your Linux system is booted. Listing active services is very easy, and you can do it with the systemctl –type=service systemctl –type=service –state=running command.

This command, when run, will show a table of all services, if they are active, as well as a brief description. To run this program, open up a terminal window. Once the terminal window is open, execute the command below.

systemctl --type=service --state=running

If you’re trying to filter through all active services for a particular service, looking through this large table can be pretty tedious. Thankfully, the systemctl command can be combined with the grep command as a filtering mechanism.

For example, to check if the “snapd.service” is running and loaded, you can run systemctl –type=service –state=running | grep ‘snapd,’ and the output will only show the Snapd service, instead of the entire table.

systemctl --type=service --state=running | grep 'snapd'

If you’d like to save the output of the table to a text file for later, use the following command. 

systemctl --type=service --state=runningrunning > ~/active-services.txt

To view the text file at any time, execute the cat command below.

cat ~/active-services.txt

Use Systemd to list inactive services

Listing active Systemd services is helpful, but what about inactive ones? Yes, it is possible to list those as well. Here’s how. First, open up a terminal window. 

Once the terminal window is open and ready to use, execute the systemctl –type=service command but in the –state= slot, place “inactive” rather than “running.” By doing this, you can see what Systemd services are inactive on your Linux system.

systemctl --type=service --state=inactive

Like with the active services, Systemd will create a nice, neat table and show you what Systemd services are inactive, dead, and not in use. If you’d like to filter through this table to find if a specific service is dead, you can combine it with the grep command.

For example, to determine if the “updatedb.service” file is inactive, you can combine systemctl –type=service –state=inactive with the grep command below to search the table for it.

systemctl --type=service --state=inactive | grep 'updatedb.service'

If you need to save all inactive Systemd service files to a text file for reading later, use the following command.

systemctl --type=service --state=inactive > ~/inactive-services.txt

To read this text file right from the terminal window, use the cat command.

cat ~/inactive-services.txt

Use Systemd to list all installed services 

If you need to see all installed Systemd services, whether they’re active or not, you’ll need to use the systemctl list-unit-files –type=service command and exclude the –state command-line switch. By excluding –state, Systemd will be able to list each and every service file on your computer with ease. 

systemctl list-unit-files --type=service

Once the above command is executed, Systemd will print out a table with every service file on the system. From here, you can sort through the table to find the service file you need. 

If you cannot find a specific service file, you can combine the systemctl list-unit-files –type=service command with the grep command to use a filter. Using the command below, filter the table to find the service file you need.

systemctl list-unit-files --type=service | grep 'my-service'

Want to export all Systemd services to a text file to read later on? Execute the following command.

systemctl list-unit-files --type=service > ~/service-files.txt

You can view the text file at any time with the cat command below.

cat ~/service-files.txt

Use Systemd to display the status of individual service

While it is helpful to know how to list all Systemd services, ones that are active and inactive, it’s also useful know how to list the status of services individually. Here’s how to do it.

First, open up a terminal window. Once the terminal window is open, use the systemctl status command on a particular service. For example, to find the status of “NetworkManager.service,” the following command is run.

systemctl status NetworkManager

If you’d like to export the status command’s output to a text file for later reading, you can enter the command below. 

systemctl status MyService > ~/MyService-status.txt

To view this file, execute the following cat command.

cat ~/MyService-status.txt

Похожие статьи

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Кнопка «Наверх»