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:

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.