How to set CPU and memory limits in systemd?

How to set CPU and memory limits in systemd?

This creates an interactive group limiting resources to 1 GB memory and 50% of a CPU core (500 ms per second of total CPU time). Then the /etc/cgrules.conf file can be set to put specific group or users under this group:

What can systemd service unit files do for You?

Systemd service unit files provide a number of high-level configuration parameters useful for resource management. These parameters communicate with Linux cgroup controllers, that have to be enabled in the kernel. With these parameters, you can manage CPU, memory consumption, block IO, as well as some more fine-grained unit properties.

Can a resource control unit be disabled in systemd?

If the system uses the legacy control group hierarchy, resource control is disabled for the systemd user instance, see systemd (1). Units of the types listed above can have settings for resource control configuration: Turn on CPU usage accounting for this unit. Takes a boolean argument.

How are CPU ranges specified in systemd resource control?

CPU ranges are specified by the lower and upper CPU indices separated by a dash. Setting AllowedCPUs= doesn’t guarantee that all of the CPUs will be used by the processes as it may be limited by parent units. The effective configuration is reported as EffectiveCPUs= .

Is there a way to limit memory per user?

There is similar question: Cgroups, limit memory per user, but the solution doesn’t work in “modern” systems, where cgroups hierarchy is managed by systemd. Straightforward solution — templating user-UID.slice — won’t work, because it is not supported, see https://github.com/systemd/systemd/issues/2556.

How to increase TASKMAX for one user in systemd?

This can be saved for example to /etc/systemd/system/loadcg.service and then enabled with systemctl enable loadcg. On Ubuntu I needed to increase the TasksMax for a single user, the CI service user (UID 2000) that runs all of my group’s tests, from the default limit of 10813 to something higher.

How to limit CPU and memory in cgroup?

Another possibility of achieving CPU or memory limitations through cgroups is to use libpam-cgroup, which can select a profile at login from /etc/cgconfig.conf. A commonly seen example is the following: This creates an interactive group limiting resources to 1 GB memory and 50% of a CPU core (500 ms per second of total CPU time).