Limiting CPU Usages in Linux using cpulimit
2 Comments
You might have already know how to check the CPU usages of any process. However, worried about any process consuming too much CPU? You should know, you can control the CPU usages of any process by CPU limiter. cpulimit is a free software to limit the CPU of any process. Here is the excepts from there site:
cpulimit is a simple program that attempts to limit the cpu usage of a process (expressed in percentage, not in cpu time). This is useful to control batch jobs, when you don’t want them to eat too much cpu. It does not act on the nice value or other scheduling priority stuff, but on the real cpu usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly.
How it works
Note that you don’t need to read this paragraph in order to use cpulimit, you can safely skip it if you are not interested in tweaking cpulimit.
So, you are curious to know the secrets of cpulimit Of course there is no secret if you are a C developer, but I will try to explain to everyone. The work of cpulimit is done all in userspace, so it doesn’t interfere with the Linux scheduler. Basically, the target process, which you can specify by pid, name, or command line, is continuosly paused and resumed by sending it SIGSTOP and SIGCONT signals. Signals are sent by cpulimit in appropriate moments, based on the limit specified by user and the process statistics read from /proc.
System Requirements
cpulimit should run on every Linux 2.2 or greater. It has been reported by several users that cpulimit works fine even on SMP hardware, but consider that if you have more than one cpu there is a little difference in the meaning of cpu usage (see below).
If you can modify the source code of cpulimit to make it run in another OS, please notify me, so I can publish your code. I think that the only non-portable code is to iterate through the process list and get process statistics.
Installation
You may download the latest from the site. Now the latest is 1.1.
You may also download from SVN source:
svn checkout https://cpulimit.svn.sourceforge.net/svnroot/cpulimit/trunk cpulimit
|
"An excellent book!" — Paul Wilmott Want to break into the lucrative world of trading and quantitative finance? You need this book! |
Then extract the source and compile with make:
zxf cpulimit-.tar.gz
cd cpulimit-
make
Usages
Limit the process pidgin by executable name to 20% CPU:
cpulimit –exe bigloop –limit 20
cpulimit –exe /usr/bin/pidgin –limit 20
Limit a process by PID to 25% CPU:
cpulimit –pid 3982 –limit 55
Launch a process by command line and limit it to 30% (in development version only!):
cpulimit –limit 30 /etc/rc.d/rc.boinc start
a WordPress rating system
Related posts:
- Secure your Linux Servers
- DoS Protection via APF, BFD, DDOS and RootKit
- Installing Suhosin by Patching PHP Source
- How do I find out Linux Resource utilization to detect system bottlenecks?
- Installing Suhosin as PHP Extension in Linux
This is a great idea. I have seen far too many sites get suspended due to over-using a server’s processor. This could save a lot of time, trouble and money for both the host and the customer. =)
a WordPress rating system