- Posted on
- • Questions and Answers
Launch a process in a new cgroup using `systemd-run --scope --user`
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Blog Article: Managing Processes with cgroups and systemd in Linux
Introduction
In this article, we'll explore the use of systemd-run --scope --user
to launch processes within a new control group (cgroup) on Linux systems, utilizing systemd's management capabilities to handle resource limitations and dependencies. This approach provides a flexible and powerful way to manage system resources at the granularity of individual processes or groups of processes.
Q1: What is a cgroup?
A: A cgroup, or control group, is a feature of the Linux kernel that allows you to allocate resources—such as CPU time, system memory, network bandwidth, or combinations of these resources—among user-defined groups of tasks (processes). They are used to organize processes hierarchically and distribute system resources along the hierarchy in a controlled and configurable manner.
Q2: What is systemd
and how does it relate to cgroups?
A: systemd is a system and service manager for Linux operating systems, which has become the standard for most Linux distributions. It initializes and manages user processes and includes native support for cgroups. systemd uses cgroups to organize processes it starts and to track them, which simplifies process management and system resource allocation.
Q3: How can I launch a process in a new cgroup using systemd-run --scope --user
?
A: To launch a process in a new cgroup, you can use the systemd-run
command with the --scope --user
flags. Here's the basic syntax:
systemd-run --scope --user [COMMAND]
This command runs the specified [COMMAND] in a new user scope, meaning it will be a new session of a user slice managed by systemd, separate from system services and isolated in its resource consumption.
Background: Simple Examples and Explanations
To better understand how systemd-run --scope --user
works, let’s consider a simple example. If we wanted to run a Bash shell (bash) in a new cgroup, you would enter:
systemd-run --scope --user -- bash
After running this, systemd will start a Bash process in a new user scope. You can check that this new scope is active and see its details by looking at the list of scopes under /sys/fs/cgroup
for your respective resource controller (like cpu or memory).
Further, you can set constraints directly from the systemd-run
command, for example, limiting memory usage to 500MB:
systemd-run --scope --user -p MemoryLimit=500M -- bash
Executable Script
Here is a basic script to demo the launching of three processes with different memory limits:
#!/bin/bash
# Create a new cgroup scope for a process with 200MB memory limit
systemd-run --scope --user -p MemoryLimit=200M -- bash -c 'echo PID: $$; exec sleep 300'
# Create a new cgroup scope for a process with 300MB memory limit
systemd-run --scope --user -p MemoryLimit=300M -- bash -c 'echo PID: $$; exec sleep 300'
# Create a new cgroup scope for a process with 400MB memory limit
systemd-run --scope --user -p MemoryLimit=400M -- bash -c 'echo PID: $$; exec sleep 300'
Conclusion
Utilizing systemd-run --scope --user
to manage Linux processes offers a granular control over system resources per process, enhancing both system efficiency and process isolation. It's a powerful tool that system administrators can leverage to maintain a balance among different workloads or users on a shared system. By understanding and using cgroups within systemd, administrators and power users can significantly improve resource management and application performance on Linux systems.
Further Reading
For further exploration of cgroups, systemd, and their capabilities in Linux, consider visiting the following resources:
Red Hat Documentation on cgroups: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/resource_management_guide/chap-using_control_groups
This official Red Hat guide provides a detailed introduction to cgroups, including their architecture and ways to configure them under different scenarios.
Understanding Systemd: https://www.freedesktop.org/wiki/Software/systemd/
Freedesktop.org offers comprehensive documentation on systemd, its components, and detailed guides on managing system and service behavior in Linux.
Linux.com Article on systemd-run: https://www.linux.com/topic/desktop/systemd-run-managing-transient-units/
This article provides practical examples and an overview of using
systemd-run
for managing transient units, which are crucial for temporary system services.DigitalOcean Tutorial on Control Groups: https://www.digitalocean.com/community/tutorials/how-to-use-control-groups-cgroups-on-ubuntu-18-04
A beginner-friendly guide that explains the basics of setting up and using cgroups on Ubuntu for resource management.
Arch Linux Wiki on Systemd: https://wiki.archlinux.org/title/Systemd
The Arch Linux wiki page on systemd is a valuable resource for understanding the role of systemd in Linux, covering everything from basic management to advanced service scripting details.
These links offer a good mix of theoretical background, practical use cases, and hands-on guides to better understand and utilize cgroups and systemd in various Linux environments.