Controlling Access to System Resources
Use the systemctl command to control a cgroup's access to system resources, for example:
sudo systemctl [--runtime] set-property httpd CPUShares=512 MemoryLimit=1G
CPUShare
controls access to CPU resources. As the default value is 1024, a
value of 512 halves the access to CPU time that the processes in the cgroup
have. Similarly, MemoryLimit
controls the maximum amount of memory that the
cgroup
can use.
Note:
You don't need to specify the .service
extension to the name of a
service.
If you specify the --runtime option, the setting doesn't persist across system reboots.
Alternatively, you can change the resource settings for a
service under the [Service]
heading in the
service's configuration file in
/usr/lib/systemd/system
. After editing the
file, make systemd
reload its configuration
files and then restart the service:
sudo systemctl daemon-reload
sudo systemctl restart service
You can run general commands within scopes and use systemctl to control the access that these transient cgroups have to system resources. To run a command within in a scope, use the systemd-run command:
sudo systemd-run --scope --unit=group_name [--slice=slice_name]
If you don't want to create the group under the default system
slice, you
can specify another slice or the name of a new slice. The following example runs a command
named mymonitor in mymon.scope
under
myslice.slice
:
sudo systemd-run --scope --unit=mymon --slice=myslice mymonitor
Running as unit mymon.scope.
Note:
If you don't specify the --scope
option, the control group is a created
as a service rather than as a scope.
You can then use systemctl to control the
access that a scope has to system resources in the same way as
for a service. However, unlike a service, you must specify the
.scope
extension, for example:
sudo systemctl --runtime set-property mymon.scope CPUShares=256
For more information see About Control Groups and the systemctl(1)
, systemd-cgls(1)
, and systemd.resource-control(5)
manual pages.