How to list Linux services with systemctl

0
279

[ad_1]

fatmawati achmad zaenuri / Shutterstock.com

To see all running services on a Linux system with systemd, use the command “systemctl –type=service –state=running”. This will show you the name, load, sub-status, and description of each active service. You can also change the status value to see services that are down, closed, failed, or down.

Your Linux computer depends on many background tasks called services or daemons. In systemd-based distributions, it has built-in commands that allow you to see what services are running, disabled, or failing.

Services and Daemons

Services and daemons are background tasks that run without a user interface, do not require human interaction, and typically start when the computer boots.

At one point, the services were launched by init, which was the first process that was launched. Services details were stored in a collection of scripts located in the “/etc/init/d” directory. On non-systemd distributions, that’s still the case.

In the systemd world, services are launched by systemd which is now the first process to be launched. Service details are stored in drive files located in the “/usr/lib/systemd” directory.

According to its man page, systemd is a systems and services administrator. you can use the systemctl command to inspect and control different aspects of the systemd system, including services and daemons.

Since we’re looking at systemd-specific commands here, the first thing you need to know is whether or not you’re running a systemd-based distribution.

RELATED: Why Linux’s systemd is still divisive after all these years

Based on init or systemd?

The vast majority of Linux distributions use systemd, including Arch, Red Hat, and Debian, and many of the distributions derived from them. That includes the Ubuntu family of distributions, Fedora and its spins, and Manjaro and the other Arch-based distributions.

However, there are forks or flavors of some of these distributions that have been created specifically to avoid having to use systemd. Not only that, but there are other init systems that someone might choose to use instead of the default one in their distribution, such as runit or s6-linux-init.

If you have to manage a Linux computer that you didn’t configure yourself, the only way to be sure whether or not you’re using systemd is to check. We can do this by looking at the process tree with the pstree domain. We only need to see the top of the tree; after all, we’re looking for the first process to run, so we’ll pipe the output through the head and ask for the first five tickets.

pstree | head -5

Use pstree piped through head to determine if a Linux installation is using systemd

we can see that systemd it’s the first process to run after boot, so we’re definitely on a systemd-based Linux installation.

RELATED: How to Manage Processes from the Linux Terminal: 10 Commands You Need to Know

Using systemctl to list services

The command to list services and daemons is systemctl. We can refine the systemctl command with the type Y state options we are asking systemctl to report on the services that are in execution status.

systemctl --type=service --state=running

Using systemctl to list running services

An information table is generated. If it’s too wide or long for your terminal window, it shows up in your default file viewer, which will probably be less.

The result of a systemctl call displayed in the file viewer less

To view the far right of the table, press the right arrow key. To return to the normal view, press the left arrow key.

The right section of the output of a systemctl call displayed in the file viewer less

Press the Q key to get out less. The columns displayed are:

  • Unit: The name of the service or daemon. The column is titled “Unit” because whatever is in this column was released using information systemd found in a drive file.
  • Burden: The load status of the service or daemon. It can be loaded, not found, misconfigured, error, or masked.
  • Active: The general state that the service or daemon is in. It can be active, reloading, inactive, failed, activating or deactivating.
  • SUB: The substate of the service or daemon. It can be dead, closed, failed, down, or running.
  • Description: A brief description of the unit.

We can pipe the output of systemctl through grep if we want to focus on a single service. This command isolates the table entry for the ssh Service.

systemctl --type=service --state=running | grep ssh

Using grep to isolate a single service from the results

So far, we have been filtering the content of the table by providing the state=running option. Instead, we can use any of the possible values ​​of the substate: dead, exited, failed, idle, or running.

Let’s look for failed services:

systemctl --type=service --state=failed

Reports on failed services with systemctl

Combinations of substates can be used. Write them as a comma-separated list. Be sure not to include any blank spaces between the options. Note that this finds matching services either condition.

systemctl --type=service --state=failed,exited

Finding services that failed or exited with systemctl

Pressing the right arrow key to view the columns off the screen shows that we have a combination of exited and failed services listed.

A mix of failed and closed services found by systemctl

Default, systemctl lists the processes (services and daemons) that have been launched by systemd because systemd found a drive file that contained a valid drive file for them. That is why the shorthand term for all these processes is “units”.

There is an option to explicitly request systemctl to list drives, but since it’s the default action, it’s not often used.

These commands produce the same results.

sudo systemctl list-units --type=service --state=running
sudo systemctl --type=service --state=running

Using systemctl to list unit files

We can expand the scope of systemctl command including the list-unit-files option. This not only reports on services and daemons that have been launched, but also lists all drive files installed on your computer.

systemctl list-unit-files --state=enabled

Listing drive files with systemctl

A color table is displayed.

A list of unit files generated by systemctl, displayed in the file explorer less

The removal of the state The option removes filtering. The output will contain all installed drive files, regardless of their status.

systemctl list-unit-files

Using systemctl to list drive files without filtering

The output will contain many more entries than the results of the previous commands.

All drive files listed by systemctl and displayed in file explorer minus

On our test computer, the list of results is almost four times as long as the output of our previous commands.

If you want to use the state option, you can use multiple states with it as we saw earlier. The same rules apply. Provide the options as comma-separated values ​​and do not include any blanks.

This command will list all the files on the drive that are disabled or could not be started.

systemctl list-unit-files --state=enabled,failed

Using systemctl to find drive files that match either state

A small number of results are displayed, filtered according to the selections made with the status option.

A mix of disabled and failed drive files found by systemctl

Looking at a service in detail

If something about a service or daemon piques your interest and deserves a deeper dive, you can view it in detail using the systemctl status option.

Let’s take a look at the SSH daemon, sshd. All we have to do is use the state option and the name of the service or daemon.

systemctl status sshd

The details of a single service displayed by systemctl

This compact screen shows:

  • The name of the service along with a short description. A color coded dot shows whether or not it is running. Green means it’s running, red means it’s not.
  • What was uploaded, including the path to the drive file.
  • How long has it been running.
  • Where the documentation is in the man Handbook.
  • The process ID of the running instance.
  • How many concurrent instances of this service are running. Usually this will be one.
  • How much memory is being consumed.
  • How much CPU time has been consumed.
  • The control group to which the service belongs.

Relevant system log entries are also displayed. These are usually events such as the start of the service. These can be informative if you are investigating a service or daemon that did not start properly.

RELATED: How to use journalctl to read Linux system logs

The Autonomous Systems

Services and daemons provide many of the automatic actions of your operating system, so they are vital. That means your health is also vital.

Getting a view of your services, daemons, and drive files is easy and informative. It’s also a valuable troubleshooting step if a service or daemon refuses to start.

RELATED: How to resolve the “Too many open files” error on Linux

[ad_2]