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 cpulimitFor accessing it’s manual, please use the below command.man cpulimitLet 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
(in Ubuntu as for this example) and flag the process and it’ll show you its ID too.Before … (although you can’t see the PID and CPU name fields as I cropped it out, but they’re in the system monitor window)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.ps -eThis should show you all the currently running process and their “PID” ORYou 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”.Mplayer in the built in “top” process viewer …Now I’d simply enter the below command to limit the CPU usage of mplayer (7%).cpulimit -e mplayer -l 7The “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 7CPUlimit analyzing the process …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.As you can see the CPU usage of mplayer is now at “7%” …That’s pretty much it. Again make sure to read ‘‘ manual before proceeding. Good luck.Related Posts
An RHCE, ‘Linux’ user with 14+ years of experience. Extreme lover of Linux and FOSS. He is passionate to test every Linux distribution & compare with the previous release to write in-depth articles to help the FOSS community.This site uses Akismet to reduce spam. .Recent Posts
ContactAbout HecticGeekWelcome to HecticGeek, it is an independent blog founded back in 2010. We cover in-depth Linux OS, product review with other all technology-related software and tools. Our mission is to provide valuable and trustful technology-related content to our users.