Skip to content

Instantly share code, notes, and snippets.

@srathi-monarch
Last active July 23, 2024 17:19
Show Gist options
  • Save srathi-monarch/a06e383a479683a6735a86b8962f497e to your computer and use it in GitHub Desktop.
Save srathi-monarch/a06e383a479683a6735a86b8962f497e to your computer and use it in GitHub Desktop.
Limit cpu usage when running a command (i.e. cgroup interface for
systemd-run --user --scope -p CPUQuota=25% --slice=stress -- stress -c 1
# Where you replace stress -c 1 with your command
# Ubuntu 20.04 doesn't support this with non root users, so you have to run
sudo systemd-run --scope -p CPUQuota=25% --slice=stress -- stress -c 1
# Note that this remove all the environment variables.
# If you want to run as the current user
sudo systemd-run --scope -p CPUQuota=25% --uid="$(id -u)" --gid="$(id -g)" --slice=stress -- id
# Dynamically change limits:
systemctl set-property stress.slice CPUQuota=5%
systemctl set-property stress.slice AllowedCPUs=0,1
# Note: you can kill the entire group like:
sudo systemctl stop stress.slice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment