Site icon DesignLinux

Listing Linux Services with Systemctl

In Linux, a service is a program that runs in the background . Services can be started on-demand or at the boot time.

If you are using Linux as your primary operating system or development, platform you will deal with different services such as webserver, ssh or, cron . Knowing how to list running services or check the service status is important when debugging system issues.

Most of the recent Linux distributions are using systemd as the default init system and service manager.

Systemd is a suite of tools for managing Linux systems. It is used to boot up the machine, manage services, automount filesystems, log events, setup hostname, and other system tasks.

This article explains how to list services in Linux.

Listing Linux Services

Systemd uses the concept of units, which can be services, sockets, mount points, devices, etc. Units are defined using text files in ini format. These files include information about the unit, its settings, and commands to execute. The filename extensions define the unit file type. For example, system service unit files have a .service extension.

systemctl is a command-line utility that is used for controlling systemd and managing services. It is part of the systemd ecosystem and is available by default on all systems.

To get a list of all loaded service units, type:

sudo systemctl list-units --type service
UNIT          LOAD      ACTIVE SUB     DESCRIPTION                                                              
cron.service  loaded    active running Regular background program processing daemon 
...

Each line of output contains the following columns from left to right:

By default, the command lists only the loaded active units. To see loaded but inactive units too, pass the --all option:

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

If you want to see all installed unit files, not only the loaded, use:

sudo systemctl list-unit-files

Displaying Service Status

To check the status of a service, use the systemctl status command:

sudo systemctl status <service_name>.service

Where <service_name> is the name of the service unit you want to check. For example to determine the current status of the nginx service you would run:

sudo systemctl status nginx.service
You can ommit the suffix “.service”. systemctl status nginx is same as systemctl status nginx.service.
● 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:

If you only want to check the service status, use the systemctl is-active command. For example, to verify that the nginx service is running, you would run:

systemctl is-active nginx.service
active

The command will show you the service status. If the service is active, the command returns an exit status of 0, which can be useful when using the command inside shell scripts.

Conclusion

We have shown you how to use the systemctl command to list Linux services and check their status.

If you have any questions or feedback, feel free to comment below.

terminal
Exit mobile version