Introduction
Managing system resources like CPU and memory is very important for maintaining a stable and efficient server. In Ubuntu, systemd resource management helps control how processes use these resources through a feature called cgroups (control groups). Two important components of this system are slices and scopes. These help organize processes and apply resource limits effectively.
In this blog, you will learn what slices and scopes are, and how to use them in a simple way.
Prerequisites
Before getting started, make sure you have:
- An Ubuntu OS
- Basic knowledge of Linux commands
- Sudo (administrator) access
Understanding Slices and Scopes
- Slices: They are used to group services together and apply shared resource limits like CPU and memory.
- Scopes: They are used to manage processes that are not started directly by systemd, such as manually executed commands.
In simple terms:
- Slices = Organizing and limiting services
- Scopes = Managing running processes
Steps to Use systemd Slices and Scopes
Step 1: View Current System Hierarchy
You can see how processes are organized using:
systemd-cgls
systemd-cgtop
Step 2: Create a Custom Slice
Create a new slice to group services:
sudo nano /etc/systemd/system/myfirst.slice
Add:
[Slice]
MemoryMax=2G
CPUQuota=200%
TasksMax=500
Reload systemd:
sudo systemctl daemon-reload
sudo systemctl start myfirst.slice
This sets limits for all services in the slice.
Step 3: Assign Services to the Slice
Edit your service file:
[Service]
Slice=myfirst.slice
Now multiple services will share the same resource limits.
Step 4: Create Nested Slices (Optional)
You can create parent and child slices to organize workloads. For example:
- app.slice
- app-web.slice
- app-workers.slice
Child slices inherit limits from parent slices.
Step 5: Use Scopes for Running Processes
Run a process inside a controlled scope:
sudo systemd-run --scope -p MemoryMax=512M -p CPUQuota=50% /path/to/app
This allows you to control resources for temporary processes.
Step 6: Monitor Resource Usage
Check usage with:
systemd-cgtop
systemctl status myfirst.slice
This helps track CPU and memory usage in real time.
Conclusion
systemd slices and scopes provide a useful way to manage system resources in Ubuntu. Slices help organize services into groups and apply shared limits, while scopes allow control of external processes. By using these features, we can ensure that important applications get enough resources and prevent others from consuming too much. We can build a more stable, efficient, and well-managed system with proper use of slices and scopes.
If you’re looking to implement systemd resource management effectively or need expert help optimizing your infrastructure, partnering with a reliable provider can make all the difference. At Skynats, our server management services and linux server management services are designed to help businesses streamline resource usage, improve performance, and maintain system stability. Get in touch with our team today to simplify your server operations and ensure your Ubuntu environment runs at its best.