Skip to Content

How to limit the CPU usage for a particular process on the CentOS server?

In Linux, you can use the cpulimit tool to restrict the CPU usage of a process. cpulimit is a simple program that attempts to limit the CPU usage of a process in percentage terms.

Please note that cpulimit is not included in the official CentOS repositories. But you can compile it from the source or find it in some third-party repositories. Once it's installed, you can use it like this:

cpulimit -p [pid] -l [percentage]
 

Here, [pid] is the process ID you want to limit, and [percentage] is the CPU usage you want to limit the process to (as a percentage of a single CPU core).

For example, if you wanted to limit process 12345 to 25% CPU usage, you'd use:

cpulimit -p 12345 -l 25
 

Remember that cpulimit doesn't lower the priority of the process, it simply stops the process periodically to ensure that it doesn't exceed the specified CPU usage. This makes it more suitable for long-running CPU-intensive tasks, rather than for system services or user-interactive applications.

Additionally, consider that cpulimit works at the granularity of a single process, and doesn't consider the usage across multiple processes from the same program or user. If you need more fine-grained control, you might want to consider using a control group (cgroup), which is a Linux kernel feature that can limit resources, including CPU, for a group of processes. This however, is a more complex setup and may require more advanced knowledge of Linux system administration.

Powered by PHPKB Knowledge Base Software