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:

  • UNIT - The name of the service unit.
  • LOAD - Information about whether the unit file has been loaded in the memory.
  • ACTIVE - The high-level unit file activation state, which can be active, reloading, inactive, failed, activating, deactivating. It is a generalization of the SUB column.
  • SUB - The low-level unit file activation state. The value of this field depends on the unit type. For example, a unit of type service can be in one of the following states, dead, exited, failed, inactive, or running.
  • DESCRIPTION - Short description of the unit file.

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:

  • Loaded - Whether the service unit has been loaded and the full path to the unit file. It also shows whether the unit is enabled to start on boot time.
  • Active - Whether the service is active and running. If your terminal supports colors and the service is active and running, the dot () and “active (running)” part will be printed in green. The line also shows how long the service is running.
  • Docs - The service documentation.
  • Process - Information about the service processes.
  • Main PID - The service PID.
  • Tasks - The number of tasks accounted for the unit and the tasks limit.
  • Memory - Information about used memory.
  • CGroup - Information about related Control Groups.

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.

Дали Ви помогна овој одговор? 237 Корисниците го најдоа ова како корисно (265 Гласови)