Linux system administrators often need to monitor and manage the services running on their systems. This task is crucial for maintaining system health, troubleshooting issues, and optimizing performance. With the widespread adoption of systemd as the init system and service manager in most modern Linux distributions, understanding how to use systemd commands to check running services has become an essential skill.
Using systemctl to List Running Services
The primary tool for interacting with systemd is the systemctl
command. This versatile utility allows you to view, manage, and control systemd services and units.
Viewing All Active Services
To get a comprehensive list of all active services on your system, use the following command:
systemctl list-units --type=service --state=active
This command displays all services that are currently loaded and active, including those that have finished executing but are still loaded in memory.
Listing Only Running Services
If you’re interested in seeing only the services that are actively running at the moment, you can modify the command slightly:
systemctl list-units --type=service --state=running
This command filters out services that have exited or are in any state other than “running”.
Understanding the Output
The output of these commands provides several columns of information:
- UNIT: The name of the service unit file
- LOAD: Whether the unit file has been loaded into memory
- ACTIVE: The high-level activation state of the unit
- SUB: More detailed information about the unit’s state
- DESCRIPTION: A brief description of the service’s purpose
Checking Individual Service Status
Sometimes you need detailed information about a specific service. Use the status
subcommand followed by the service name:
systemctl status service_name
Replace service_name
with the actual name of the service you want to check. This command provides comprehensive information, including:
- Whether the service is active or inactive
- The process ID (PID) if the service is running
- Recent log entries related to the service
Filtering Services by State
Systemd allows you to filter services based on their state. Here are some useful variations:
-
List failed services:
systemctl list-units --type=service --state=failed
-
List services that have exited:
systemctl list-units --type=service --state=exited
-
List services set to start at boot:
systemctl list-unit-files --type=service --state=enabled
Creating Custom Service Lists
For system administrators who frequently need to check specific sets of services, creating aliases can be a time-saver. Add these to your .bashrc
or .bash_aliases
file:
alias list-running='systemctl list-units --type=service --state=running'
alias list-failed='systemctl list-units --type=service --state=failed'
After adding these aliases, you can use list-running
or list-failed
as quick commands to check services.
Checking Service Ports
To determine which ports a service is using, combine systemctl
with the ss
command:
systemctl status service_name | grep PID
ss -tulpn | grep <PID>
Replace service_name
with the name of your service, and <PID>
with the process ID obtained from the first command.
Monitoring Service Resource Usage
For a more dynamic view of service resource consumption, you can use the systemd-cgtop
command:
systemd-cgtop
This command provides a top-like interface showing CPU, memory, and disk I/O usage for systemd services and other units.
Regularly checking and managing running services is a crucial part of Linux system administration. By mastering these systemd commands, you’ll be better equipped to maintain system stability, troubleshoot issues, and optimize performance. Remember to consult the systemctl
man pages for more advanced usage and options as you become more comfortable with these basic commands.