Limiting the CPU usage of a process (or processes) can be quite useful at time. For instance, let’s say that you were compiling a program and didn’t want it to use a lot of your CPU so it wouldn’t interrupt your other applications. But if you don’t know what you’re doing, then it might even break the process, even your OS! (so you should always be careful while using them).
By default it is the Kernel of your OS that handles processes and their CPU usage, depending on their importance. But there’s a simple tool called “CPUlimit” that lets us manually limit the CPU usage of a given process.
This is a command-line tool and before limiting the CPU usage you’ll have to know the name or the “ID” of that process. Once you know it, you can use CPUlimit and control the CPU usage easily.
First you can install “CPULimit” in Ubuntu 12.04 Precise Pangolin, 11.10 Oneiric Ocelot, 11.04 Natty Narwhal, 10.10 and 10.04 by typing the below command in your Terminal window.
sudo apt-get install cpulimit
For accessing it’s manual, please use the below command.
Let me give you a quick example on how to use it.
1. Let’s assume that after opening “mplayer” (awesome, cross-platform multimedia player!) for opening a video file, I just wanted to make sure that it only uses about 7% of my CPU. Then first I have to know its name or it’s PID (process ID).
The easiest way is to use your default system monitor (in Ubuntu as for this example) and flag the process and it’ll show you its ID too.
If you don’t want to use a graphical tool at all then you can use the built in commands like “ps” for finding the process.
In that case, open your Terminal window and enter the below command which shows a list of currently running processes.
This should show you all the currently running process and their “PID” OR
You can use another command called “top” which only shows the top processes by their CPU usage. This is useful most of the time to flag an already running process as it filers out the rest (the “ps” command too can do that).
Anyhow after getting either the actual name of the process or its ID you can use that information to limit the CPU usage. Now as you can see with the above screenshot, the process name in this example is “mplayer” and the ID is “7756”.
Now I’d simply enter the below command to limit the CPU usage of mplayer (7%).
cpulimit -e mplayer -l 7
The “e” parameter is only if you know the actual name of the running app and “l” (simple “L”) is for entering the CPU usage limit.
If I wanted to do the same but this time only using the actual PID of mplayer, then I’d use the below command instead of the above one.
cpulimit -p 7756 -l 7
Does it work?
Heck ya it does! :D. But as said before you don’t want to limit the CPU use aggressively otherwise it can break the process (which happened to me once, as I was playing a video file and reduced the CPU usage quite aggressively, yikes!). And for the maximum efficiency, you might wanna run it with administrative (sudo) privileges too.
That’s pretty much it. Again make sure to read ‘CPUlimit‘ manual before proceeding. Good luck.