Listing Linux Services with Systemctl

Before we begin talking about listing Linux Services with Systemctl, let's briefly understand – What is Systemctl?

systemctl is a command-line utility in Linux systems that is used to manage and control the systemd service manager. Systemd is a system and service manager for Linux that provides a way to manage and control various aspects of the system, such as starting or stopping services, monitoring their status, and managing system processes.

A service on Linux is a program that runs in the background. Services can be launched on demand or at boot time.

If you use Linux as your primary operating system or development platform, you will have to deal with various services such as webserver, ssh, and cron. When debugging system issues, knowing how to list running services or check the service status is essential.

Systemd is the default init system and service manager in the majority of recent Linux distributions.

Systemd is a collection of tools for administering Linux systems. It is responsible for booting up the machine, managing services, auto-mounting filesystems, logging events, configuring the hostname, and other system tasks.

In this tutorial, you will list Linux Services with Systemctl. We will also address a few FAQs on Listing Linux Services with Systemctl.

Listing Linux Services

Systemd employs the unit concept, which can refer to services, sockets, mount points, devices, and so on. Text files in ini format are used to define units. These files contain information about the device, it's settings, and commands to run. The unit file type is defined by the filename extensions. System service unit files, for example, have a .service extension.

systemctl is a command-line utility for managing services and controlling systemd. It belongs to the systemd ecosystem and is installed by default on all systems.

Type the following command to get a list of all loaded service units.

sudo systemctl list-units --type service
Output

UNIT          LOAD      ACTIVE SUB     DESCRIPTION                                                              
cron.service  loaded    active running Regular background program processing daemon 
...

From left to right, each line of output contains the following columns:

  • UNIT - The service unit's name.
  • LOAD - Whether or not the unit file has been loaded into memory.
  • ACTIVE - The high-level activation state of a unit file, which can be active, reloading, inactive, failed, activating, or deactivating. It is a generalization of the SUB column.
  • SUB - The activation state of a low-level unit file. This field's value is determined by the unit type. A service unit, for example, can be in one of the following states: dead, exited, failed, inactive, or running.
  • DESCRIPTION - Brief explanation of the unit file.

By default, the command displays only the currently loaded active units. Pass the --all option to see loaded but inactive units as well:

sudo systemctl list-units --type service --all

If you want to see all installed unit files, not just the ones that are loaded, type:

sudo systemctl list-unit-files

Displaying Service Status

Use the systemctl status command to determine the status of a service:

sudo systemctl status <service_name>.service

Where <service_name> is the name of the service unit to be examined. To determine the current status of the nginx service, for example, run:

sudo systemctl status nginx.service
ℹ️
The suffix ".service" can be omitted. The command systemctl status nginx is equivalent to systemctl status nginx.service.
Output

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2020-12-23 19:13:50 UTC; 5s ago
       Docs: man:nginx(8)
    Process: 3061052 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 3061063 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 3061064 (nginx)
      Tasks: 2 (limit: 470)
     Memory: 6.0M
     CGroup: /system.slice/nginx.service
             ├─3061064 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─3061065 nginx: worker process

Dec 23 19:13:50 linuxize.dev systemd[1]: Starting A high performance web server and a reverse proxy server...

The command will print the following information:

  • Loaded - The service unit's status and the full path to the unit file. It also indicates whether the unit is set to boot up automatically.
  • Active - Whether the service is active and functional. If your terminal supports colours and the service is active and running, the dot () and "active (running)" will be printed in green. The line also shows how long the service has been operational.
  • Docs - The service documentation.
  • Process - Information about the service processes.
  • Main PID - The service PID.
  • Tasks - The task limit of the unit and the number of tasks accounted for
  • Memory - Information about used memory.
  • CGroup - Information about related Control Groups.

If all you want to do is check a service's status, use the systemctl is-active command. For example, to ensure that the nginx service is up and running, execute:

systemctl is-active nginx.service
Output

active

The command displays the status of the service. If the service is running, the command returns a 0 exit status, which is useful when using it in shell scripts.

FAQs to Listing Linux Services with Systemctl

How can I list all services managed by Systemctl? 

Use the command systemctl list-units --type=service to display a list of all active and inactive services on your Linux system.

How do I check the status of a specific service?

Use the command systemctl status <service-name> to check the status of a specific service, replacing <service-name> with the name of the service you want to examine.

Can I list only active or inactive services? 

Yes, you can use systemctl list-units --type=service --state=active or systemctl list-units --type=service --state=inactive to list only active or inactive services, respectively.

What does the output of systemctl list-units show? 

The output displays information such as the unit name, load status, active status, and description of each service or unit.

How can I view the detailed description of a service? 

Use the systemctl show <service-name> command to display detailed information about a specific service, including its dependencies, configuration, and more.

How can I list all failed services in Systemctl? 

Execute systemctl list-units --type=service --state=failed to get a list of all services that have failed to start or encountered an error.

Can I check the startup time of a service with Systemctl? 

Yes, you can use the systemctl show <service-name> --property=ExecMainStartTimestamp command to determine the startup time of a particular service.

Conclusion

We demonstrated how to use the systemctl command to list and monitor the status of Linux services.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.