In Linux, you can limit the CPU usage of a process by using the cpulimit
command. Here’s how to use it:
- Install
cpulimit
on your Linux system, if it is not already installed. You can do this using your distribution’s package manager. For example, on Ubuntu or Debian, you can run:sudo apt-get install cpulimit
- Identify the process ID (PID) of the process you want to limit. You can use the
pidof
command to get the PID of a running process. For example, to get the PID of thefirefox
process, you can run:pidof firefox
- Use the
cpulimit
command to limit the CPU usage of the process. The basic syntax of the command is as follows:cpulimit -l LIMIT -p PID
Replace
LIMIT
with the maximum percentage of CPU usage you want to allow for the process, andPID
with the PID of the process you want to limit.For example, to limit the
firefox
process to 50% CPU usage, you can run:cpulimit -l 50 -p $(pidof firefox)
This will limit the
firefox
process to using a maximum of 50% of the CPU.Note that
cpulimit
works by pausing and resuming the process at a given frequency, so the actual CPU usage may fluctuate around the limit you set. - If you want to remove the CPU usage limit, you can kill the
cpulimit
process or use thekillall cpulimit
command to kill allcpulimit
processes.
That’s it! You have successfully limited the CPU usage of a process in Linux using cpulimit
.