How to Limit CPU on a VPS?

Limiting CPU usage on Virtual Private Servers (VPS) is an important task that can help you manage your server’s resources more effectively. If your VPS is used for hosting a website, application, or other services, CPU limiting can help you avoid situations where one process overloads the server and slows down other applications.

There are several ways to limit CPU usage on your VPS. One of them is using the cpulimit utility, which allows you to limit CPU usage for a specific process or group of processes.

To install and use cpulimit, follow these steps:

  • apt-get update — update the package list
  • apt-get install cpulimit — install the cpulimit utility
  • cpulimit -l 50 -p PID — limit the process with the specified PID to 50% CPU usage

If you want to limit the process using a specific amount of CPU, replace the value 50 with the value you need as a percentage.

VPS хостинг

Виртуальные серверы с гарантированными ресурсами

Выбрать VPS

Another way to limit CPU on a VPS is using cgroups. Cgroups allow you to create process groups and set limits on their resource usage, including CPU.

To limit CPU usage using cgroups, follow these steps:

  • mkdir /sys/fs/cgroup/cpu/MyGroup — create a cgroup group
  • echo 50000 > /sys/fs/cgroup/cpu/MyGroup/cpu.cfs_quota_us — set a limit of 50,000 CPU quotas for the MyGroup group
  • cgcreate -g cpu:/MyGroup — create cgroup MyGroup with CPU limit
  • cgclassify -g cpu:/MyGroup PID — add the process with the specified PID to the MyGroup group

Now you know how to limit CPU usage on your VPS to optimize your server’s performance. Use the suggested methods and choose the one that best suits your project.