Understanding the Command 'systemd-cgls' (with examples)
- Linux
- December 17, 2024
The systemd-cgls
command is an essential tool in the Linux environment, particularly for system administrators or power users who need to monitor and manage system processes and resources. This command provides a hierarchical view of the control groups (cgroups) on a system, displaying them in a tree format. Control groups are a Linux kernel feature that organizes processes hierarchically and distributes system resources along the hierarchy in a controlled and efficient manner. By using systemd-cgls
, users can identify how resources are allocated across different processes and services, understand the system load, and troubleshoot performance issues.
Use case 1: Display the whole control group hierarchy on your system
Code:
systemd-cgls
Motivation:
This basic usage of the systemd-cgls
command is highly beneficial for system administrators who want to gain a comprehensive snapshot of the entire control group hierarchy on their Linux systems. It offers a complete overview of processes and how they are organized under different control groups. This is particularly useful during system audits, performance analysis, and when trying to understand how system resources are being distributed.
Explanation:
- systemd-cgls: This command, when executed without any additional arguments, will scan the ‘/sys/fs/cgroup/’ directory and display the full tree of control groups. It shows all processes running on the system in a structured hierarchy, highlighting how they are grouped and managed.
Example Output:
Control group /:
-.slice
├─user.slice
│ ├─user-1000.slice
│ │ ├─session-2.scope
│ │ │ ├─2345 sshd: user
│ │ │ └─2346 -bash
└─system.slice
├─sshd.service
│ └─2763 sshd: user [priv]
└─dbus.service
└─1115 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
Use case 2: Display a control group tree of a specific resource controller
Code:
systemd-cgls cpu|memory|io
Motivation:
Sometimes, system administrators are only interested in monitoring specific resources, such as CPU, memory, or I/O, especially when there are resource constraints or when optimizing specific aspects of system performance. Using this command with an argument like cpu
, memory
, or io
enables administrators to narrow down the tree view to show control groups related to the specified controller, assisting in focused troubleshooting and resource distribution insights.
Explanation:
- cpu: When specified, the command shows the tree for control groups that are governed by CPU allocation policies.
- memory: This argument filters the view to control groups managing memory limits and usage.
- io: Using this argument will display the control groups related to input/output operations.
Example Output:
When using systemd-cgls cpu
, the output might look like:
Control group /:
cpu
├─user.slice
│ ├─user-1000.slice
│ │ ├─session-2.scope
│ │ │ ├─2345
│ │ │ └─2346
...
Use case 3: Display the control group hierarchy of one or more systemd units
Code:
systemd-cgls --unit unit1 unit2 ...
Motivation: In systems using systemd, various services and applications are managed as units. At times, it is crucial for an administrator to trace or debug issues with specific units or determine how their resources are managed. By specifying one or more systemd units, this command lets users filter the display to only include control groups associated with the specified units. This targeted inspection aids in quicker diagnostics and understanding resource impacts.
Explanation:
- –unit: This option tells the command to limit the output only to control groups associated with the provided systemd unit names, allowing for precision in monitoring specific services or applications.
Example Output:
If running the command with --unit sshd.service
might yield:
Control group /:
system.slice
└─sshd.service
└─2763
Conclusion:
The systemd-cgls
command is a potent utility for analyzing and monitoring the process hierarchy managed through Linux control groups. Its versatile use cases allow users to view both broad and narrow scopes of system resource allocation, ensuring systems run optimally. Whether examining the entire control group structure, focusing on specific resources, or isolating particular systemd units, systemd-cgls
provides critical insights into process management on a Linux system.