Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for using systemd-run to limit CPU and memory usage of multiple processes?

I would like to use Linux control groups (cgroups) to limit CPU and RAM usage of multiple processes. If the processes are Process 1 and Process 2 with child process Process 2 Sub, would this be the correct, minimal command syntax:

sudo systemd-run --scope -p MemoryHigh=200M Process\ 1
                         -p MemoryHigh=200M Process\ 2
                         -p MemoryHigh=200M Process\ 2\ Sub
                         -p MemoryMax=250M Process\ 1
                         -p MemoryMax=250M Process\ 2
                         -p MemoryMax=250M Process\ 2\ Sub
                         -p CPUQuota=10% Process\ 1
                         -p CPUQuota=10% Process\ 2
                         -p CPUQuota=10% Process\ 2\ Sub

I'm uncertain about the correct process ID (full path? process name?), the "chaining" of options, case sensitivity, escaping blank spaces, if child processes (here: Process 2 Sub) are automatically included in the imposed limits or need to be controlled individually, and if this command needs to be run every time the process in question is started or if the settings are permanent. Thx!

like image 423
david Avatar asked Sep 14 '25 03:09

david


1 Answers

systemd-run creates a new scope and launches a program inside of it.

The syntax to use is:

systemd-run --scope -p MemoryMax=250M -p MemoryHigh=200M /path/to/program/to/use
like image 171
Giuseppe Scrivano Avatar answered Sep 19 '25 15:09

Giuseppe Scrivano