Chapter 2. Using Control Groups
2.1. Creating Control Groups
systemd-runcommand. This way, it is possible to set limits on resources consumed by the service during its runtime. Applications can create transient cgroups dynamically by using API calls to systemd. See the section called “Online Documentation” for API reference. Transient unit is removed automatically as soon as the service is stopped.
2.1.1. Creating Transient Cgroups with systemd-run
systemd-runcommand is used to create and start a transient service or scope unit and run a custom command in the unit. Commands executed in service units are started asynchronously in the background, where they are invoked from the systemd process. Commands run in scope units are started directly from the
systemd-runprocess and thus inherit the execution environment of the caller. Execution in this case is synchronous.
systemd-run --unit=name --scope --slice=slice_name command
- The name stands for the name you want the unit to be known under. If
--unitis not specified, a unit name will be generated automatically. It is recommended to choose a descriptive name, since it will represent the unit in the
systemctloutput. The name has to be unique during runtime of the unit.
- Use the optional
--scopeparameter to create a transient scope unit instead of service unit that is created by default.
- With the
--sliceoption, you can make your newly created service or scope unit a member of a specified slice. Replace slice_name with the name of an existing slice (as shown in the output of
systemctl -t slice), or create a new slice by passing a unique name. By default, services and scopes are created as members of the system.slice.
- Replace command with the command you wish to execute in the service unit. Place this command at the very end of the
systemd-runsyntax, so that the parameters of this command are not confused for parameters of
systemd-run. For example,
--descriptioncreates a description of the unit,
--remain-after-exitallows to collect runtime information after terminating the service's process. The
--machineoption executes the command in a confined container. See the
systemd-run(1) manual page to learn more.
Example 2.1. Starting a New Service with systemd-run
test. Type as
systemd-run --unit=toptest --slice=
Running as unit toptest.service
2.1.2. Creating Persistent Cgroups
systemctl enablecommand (see the chapter called Managing Services with systemd in Red Hat Enterprise Linux 7 System Administrators Guide). Running this command automatically creates a unit file in the
/usr/lib/systemd/system/directory. To make persistent changes to the cgroup, add or modify configuration parameters in its unit file. For more information, see Section 2.3.2, “Modifying Unit Files”.