Sample Header Ad - 728x90

Can systemd-run create temporary cgroups for temporary commands with resource limits?

1 vote
0 answers
377 views
I'd like to limit the resource consumption of temporary commands with cgroup created by systemd-run, similar to what cgcreate does instead, but I'm not sure if that makes sense? I'd like to make sure I'm trying this correctly, thank you all!
┌──[root@liruilongs.github.io]-[~]
└─$ systemd-run -p  MemoryLimit=5M  -p CPUShares=100 --unit=sleep-50 --slice=test sleep 50
Running as unit sleep-50.service.
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl status sleep-50.service
● sleep-50.service - /usr/bin/sleep 50
   Loaded: loaded (/run/systemd/system/sleep-50.service; static; vendor preset: disabled)
  Drop-In: /run/systemd/system/sleep-50.service.d
           └─50-CPUShares.conf, 50-Description.conf, 50-ExecStart.conf, 50-MemoryLimit.conf, 50-Slice.conf
   Active: active (running) since 六 2022-10-29 01:29:29 CST; 10s ago
 Main PID: 33234 (sleep)
   Memory: 92.0K (limit: 5.0M)
   CGroup: /test.slice/sleep-50.service
           └─33234 /usr/bin/sleep 50

10月 29 01:29:29 liruilongs.github.io systemd: Started /usr/bin/sleep 50.
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl cat sleep-50.service
# /run/systemd/system/sleep-50.service
# Transient stub

# /run/systemd/system/sleep-50.service.d/50-CPUShares.conf
[Service]
CPUShares=100
# /run/systemd/system/sleep-50.service.d/50-Description.conf
[Unit]
Description=/usr/bin/sleep 50
# /run/systemd/system/sleep-50.service.d/50-ExecStart.conf
[Service]
ExecStart=
ExecStart=@/usr/bin/sleep "/usr/bin/sleep" "50"
# /run/systemd/system/sleep-50.service.d/50-MemoryLimit.conf
[Service]
MemoryLimit=5242880
# /run/systemd/system/sleep-50.service.d/50-Slice.conf
[Service]
Slice=test.slice
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl status sleep-50.service
Unit sleep-50.service could not be found.
┌──[root@liruilongs.github.io]-[~]
└─$
Asked by 山河以无恙 (185 rep)
Oct 28, 2022, 05:43 PM