How to List Services on Ubuntu 22.04?


In Ubuntu (or any other Linux distribution), services are the core manager of most of the programs. The services manage (start/stop/dead the specific program/application) the programs from the backend. These services are categorized with their respective status, i.e., stopped services, running services, etc. The users can list down the services list to check the status (stopped, running, dead/inactive) of the specific program/application. Based on the status of the service, the applications can be managed accordingly, i.e., start the service if it needs to be running, stop the service, etc.

Keeping the importance in view, this post will address the potential methods to list the services on Ubuntu 22.04.

 

Initialization(Init) Systems of Services on Ubuntu 22.04

The services on Ubuntu 22.04 refer to a specific initialization system. Each initialization system has a specific directory where the services are placed and each init system is triggered by a specific command. Let’s see, what init systems are there on Ubuntu 22.04:

  • Systemd (Latest): This is the most used and modern init system of all the others supported. The “systemd” dependent services are placed in the file “/lib/systemd” of the system. Moreover, the “systemctl” command is used to manage the services. The “systemd” was made effective from “Ubuntu 14.04 LTS”.
  • System V or SysV (Old But Still Supported): System V is another initialization system on Ubuntu 22.04. The services that use SysV as the initialization system reside in the file “/etc/init.d” and the “service” command is used to manage these services.

How to Check the Initialization System on Ubuntu 22.04?

Before you get into listing the services, you must know which initialization system your system is using and then you can choose the relevant command to list the services. To check the init system of your Ubuntu 22.04, use one of the following commands:

The following command lists the command associated with the process that has PID=1. Usually, the PID=1 refers to the init system:

ps -p 1 -o comm=

As per the output, the “systemd” is the initialization system on our Ubuntu 22.04. Similarly, the command below also provides the init system through which the system processes are booted up:

stat /sbin/init

The output shows that the “/sbin/init” has a symbolic link over “/lib/systemd/systemd” which authenticates that the “systemd” is our initialization system.

 

How to List the Services on Ubuntu 22.04?

Once you have verified your initialization step, you must know the command for that specific Init system. For instance, the “systemctl” command refers to the “systemd” and the “service” command refers to the “SysV” init system. We will elaborate on both the commands to list the services:

Note: Although the “systemctl” command refers to the latest init supported on Ubuntu 22.04. However, the “service” command offers the high-level usage of all the “init” systems, which states that the “service” command can also handle the services relevant to the “systemd” init system.

List All Services on Ubuntu 22.04

  1. Using systemctl Command
$ sudo systemctl --type service --all

Note: By default, the “systemctl” command shows the output in either less/more format (displaying one page at a time). To avoid this and get the overall output at once, you can use the “–no-pager” flag with the “systemctl” command.

In the output:

  • The “Unit” (first column) points toward the exact name through which a service is recognized (managed) by the system. While the “Description” (last column) shows the more understandable name of that service.
  • The “load” represents the state of the service configuration (either loaded or not). If the “systemctl” finds the daemon in the “systemd” then it marks it as loaded, else if it fails to trace then it is not loaded.
  • The “Active” shows the active/inactive/failed status of the service.
  • The “SUB” denotes the sub-state of the Active/Inactive/Failed services. Like, they are running actively, active but not running, etc.

Understanding the Entries of “Active” and “SUB” Fileds

  • Active and Running: The service is active and in a running state.
  • Inactive and Dead: The service is stopped.
  • Active and Exited: These are called one-shot services, these services run for the first time, do their job, and then exit. In short, there is no process running that depends on these services.

Note: To get the status of the service a bit changed from the above output, you can list the unit files as follows:

systemctl --type service list-unit-files

The output shows the current state of the specific unit (the name through which the service is recognized) and the first-time status of the service when it was installed/shipped.

  1. Using the Content of /lib/systemd

As discussed, the “/lib/systemd” contains the unit files of the systemd services directory. Thus, you can list the files inside the “lib/systemd” to get the services on your system:

ls /lib/systemd

  1. Using the service Command

The column with the pipe is just used to organize the output:

$ service --status-all | column

The “[+]” and “[-]” represent the running and stopped services respectively.

Note: If you see, the “service” command shows a very basic output (with the least services) whereas the “systemctl” command has provided a brief output (with most of the services). Thus, if your Ubuntu has “systemd” as the initialization system then you must use “systemctl” to manage the services.

  1. Using the Content of ~/etc/init.d

You can list down the services that reside in the “/etc/init.d” directory (“SysV” supported services are listed here) as well:

ls /etc/init.d

Note: The services shown in the above output have no on-screen status. You cannot differentiate which services are running, stopped, enabled, disabled, etc.

List Only Running Services on Ubuntu 22.04

  1. Using the systemctl Command

The “–type service” represents that services are being fetched. While the “–state running” only prints the running services on the system.

sudo systemctl --type service --state running

  1. Using the pstree Command | Only Applicable to systemd Services

The “pstree” command also lists the running services associated with the “systemd” init system. Use it as follows:

$ pstree

All the running services are organized in a tree-shaped structure.

  1. Using the service Command
service --status-all | grep '\[ + \]'

List the Only Active(Exited) Services on Ubuntu 22.04

This scenario is possible in the case of the “systemd” based init system. Thus the “systemctl” command to be used in the following way:

$ sudo systemctl --type service --state exited

List Only Stopped/Inactive/Dead Services on Ubuntu 22.04

  1. Using the systemctl Command

In the “systemd” init system, the inactive services are also referred to as stopped.

sudo systemctl --type service state inactive

  1. Using the service Command
service --status-all | grep '\[-\]'

The “[-] (stopped)” services are displayed on the terminal. The service command does not provide extra information such as, dead/inactive as “systemctl” shows.

That’s how you can list the services as a whole or on a specific parameter. Want to learn how a service is managed on Ubuntu 22.04? Let’s go through this:

 

How to Manage Services on Ubuntu 22.04?

The “systemctl” and “service” commands provide more or less the same functionality depending upon the “Init” system. There are some basic commands to manage services using any of these utilities:

Note: Check your init system and then choose either “systemctl” (For systemd init system) or “service”(For SysV init System).

 

Manage Services Using systemctl Command

The systemctl command has the following syntax to manage the services:

sudo systemctl <Options> <service-name>

Options

  • start”: Start the service.
  • enable”: To enable (the service will keep on running whenever a system starts).
  • disable”: Disable the service. The service will not be able to start when the system reboots.
  • restart”: Stop and Start the service.
  • reload”: Reloads the configurations associated with the service (only applicable when the service is in active state).
  • status”: Check the overall status, i.e., service is running/stopped/enabled/disabled.

Note: The systemctl has very extensive usage. However, we have only considered the primary options to manage a service.

Example

Look at the following output, we have used the “NetworkManager” as a service and applied various systemctl options, i.e., start/stop/enable/disable.

Similarly, you can restart, reload, and check the status of the service in the same manner.

Manage the Service Using the service Command

The service command has a bit different syntax from the systemctl:

sudo service <service-name> <options>

Options

  • start/stop/”: To start or stop the service.
  • “restart/–full-restart”: Restart the service components (stopping and starting).
  • reload/force-reload”: Restart/Reload the configurations of the service.
  • status”: To check the current status of the service.

Example:

As an example the service named “cron” is practiced using the service command:

 

Bottom Line

Listing the services on Ubuntu depends on the Init system of your machine. If it is “systemd” then use the “systemctl” command and if it is “SysV” then use the “service” command. Most of the latest Ubuntu systems are equipped with the “systemd” as their Init systems.

The systemctl command is the modern and most used utility to manage the systemd services. On the other hand, the “service” command offers a high-level view and does support limited functionality to deal with the “systemd” services. Thus, it is recommended to use the “systemctl” command for “systemd” dependent services.

This post has demonstrated the methods to list the services on Ubuntu 22.04.

 

Print Friendly, PDF & Email
Categories