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.
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 .
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.
Install cpulimit in debian
#apt-get install cpulimit
Examples of use
Limit the process ‘bigloop' by executable name to 40% CPU:
Limit a process by PID to 55% CPU: cpulimit
--limit 55 Launch a process by command line and limit it to 40% (in development version only!):
--limit 40 /etc/rc.d/rc.boinc start
-- limits the CPU usage of a process
cpulimit TARGET [OPTIONS...]
TARGET must be exactly one of these:
--pid=N pid of the process
--exe=FILE name of the executable program file
--path=PATH absolute path name of the executable program file
--limit=N percentage of CPU allowed from 0 to 100 (mandatory)
--verbose show control statistics
--lazy exit if there is no suitable target process, or if it dies
--help display this help and exit
Assuming you have started ‘foo --bar‘ and you find out with top or ps that this process uses all your CPU time you can either
# cpulimit -e foo -l 50
limits the CPU usage of the process by acting on the executable program file (note: the argument “
--bar” is omitted)
# cpulimit -p 1234 -l 50
limits the CPU usage of the process by acting on its PID, as shown by ps
# cpulimit -P /usr/bin/foo -l 50
same as -e but uses the absolute path name