Before we begin talking about systemctl
let's briefly understand - What is systemctl
?
systemctl
is a command that examines and controls the state of the systemd
service manager and system.
It provides a comprehensive and efficient way to list, start, stop, enable, and disable services.
Understanding how to effectively use systemctl will help you navigate and manage services on your Linux system efficiently.
We will explore various options and syntax to query and display detailed information about services, including their status, running state, and dependencies.
In this tutorial, we will introduce the usage of the systemctl command to list Linux services. We will also address a few FAQs on how to List Linux Services with Systemctl.
List Linux Services
Units are used by Systemd that might be services, sockets, mount points, devices, and so forth. Text files in the ini
format are used to define units. These files include data about the device, its settings, and commands to run. The unit file type is determined by the filename extensions. The .service
extension, for example, is used for system service unit files.
systemctl
is a command-line utility for controlling and managing systemd services. It's part of the systemd ecosystem and comes pre-installed on all systems.
Use the following command to see 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 has the following columns:
UNIT
- The service unit's name.LOAD
- Indicates whether the unit file was loaded into memory.ACTIVE
- The activation state of a high-level unit file, which can be active, reloading, inactive, failing, activating, or deactivating. It's a broadening of the SUB column.SUB
- The activation status of the low-level unit file. This field's value is determined by the unit type. A service unit, for example, can be in any of the states: dead, exited, failed, inactive, or operating.DESCRIPTION
- A brief summary of the unit file.
The command lists only the loaded active units by default. Pass the --all
option to view both loaded and inactive units:
sudo systemctl list-units --type service --all
Use the following command to display all installed unit files, not just the loaded ones:
sudo systemctl list-unit-files
Displaying Service Status
Use the systemctl
status command to check the status of a service:
sudo systemctl status <service_name>.service
The name of the service unit you want to examine is <service_name>
. To check the current status of the Nginx service, for example, type:
sudo 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 following information will be printed by the command:
Loaded
: Whether the service unit has been loaded, as well as the entire path to the unit file. It also shows whether the unit is set to start automatically when the computer boots up.
Active
- Indicates whether the service is up and functioning. The dot () and "active (running)" parts will be written in green if your terminal supports colors and the service is active and running. The line also displays the duration of the service.
Documentation
- The service documentation.
Process
- This section contains information about the service processes.
Main PID
- The service PID is the main PID.
Tasks
- The number of tasks that the unit is responsible for, as well as the task limit.
Memory
- Information about the memory that has been used.
CGroup
- Information about related Control Groups.
If all you want to do is check the status of a service, use the systemctl is-active
command. To test that the Nginx service is up and running, run:
systemctl is-active nginx.service
Output
active
The command will display the current status of the service. The command gives an exit status of 0 if the service is active, which is beneficial when utilizing it in shell scripts.
FAQ's on how to list Linux services with systemctl
How do I list services that are set to start at boot?
To list services that are set to start at boot, use the systemctl list-unit-files
command and filter the output using --type=service
and --state=enabled
.
Can I customize the output format when listing services with systemctl?
Yes, you can customize the output format using the --output
option followed by formatting directives. For example, --output=csv
or --output=json
.
How can I list services that have failed or encountered errors?
To list services that have failed, use the --state=failed
option or --state=error
.
How can I check the dependencies of a specific service?
Use the systemctl list-dependencies
command followed by the service name to see a list of dependencies for that service.
Can I restart a service using systemctl?
Yes, you can restart a service using the systemctl restart
command followed by the service name: systemctl restart service_name
.
Can I list services filtered by a specific property?
Yes, you can filter services by a specific property using the --property
option. For example, --property=MainPID
will list services based on their main process ID.
How can I list services that are running for a specific user?
Use the --user
option followed by the username to list services that are running under that specific user. For example, systemctl --user list-units --type=service
.
Conclusion
With its various options and flexibility, you can easily list services, view their status, filter based on different criteria, and perform actions such as starting, stopping, enabling, and disabling services.
By understanding how to use systemctl effectively, you can efficiently navigate and manage services on your Linux system, ensuring the smooth operation of your applications and processes.
We hope this detailed guide helped you to list Linux services with systemstl
.
If you have any queries, please leave a comment below and, we’ll be happy to respond to them for sure.