Re: HOWTO: Set maximum CPU consumption in percentage by any process
Quote:
Originally Posted by
abcuser
Hi,
OK, now I understand. The idea would probably go in this way. At boot time you start some script to start you program and after that some kind of cpulimit daemon has to start and try to catch PID of your desired program and lower the CPU of that program.
I have written this script some time ago, so it should be something like this (instead of cpulimit daemon write this script)
#!/bin/bash
cpulimit -p $( ps -eo pid,args | grep "process name you would like to CPU limit" | grep -v grep | gawk '{print $1}' ) -l 5 -z &
Note: instead of string "process name you would like to CPU limit" write your process name getting from "ps -eo pid, args" command. Above script assumes you would like to lower CPU to 5% (string: "-l 5" at the end of command), if you would like any other limit change this settings.
After that you have to make sure that your program has to start first and then this new cpulimit script goes second. Assuming you scrip has 20 priority which is by default, run the following script:
update-rc.d cpulimit-daemon defaults 21 19
Note: cpulimit-daemon is your script file name (select appropriate name) to start cpulimit daemon.
Hope this helps,
Regards
That helps a lot, thank you. There is an issue with your ps/grep combo...
Here's the output from the ps and grep:
Code:
6365 sh -c ./FahCore_a0.exe -dir work/ -suffix 01 -checkpoint 15 -verbose -lifeline 6361 -version 602
6366 ./FahCore_a0.exe -dir work/ -suffix 01 -checkpoint 15 -verbose -lifeline 6361 -version 602
6367 ./FahCore_a0.exe -dir work/ -suffix 01 -checkpoint 15 -verbose -lifeline 6361 -version 602
6368 ./FahCore_a0.exe -dir work/ -suffix 01 -checkpoint 15 -verbose -lifeline 6361 -version 602
6369 ./FahCore_a0.exe -dir work/ -suffix 01 -checkpoint 15 -verbose -lifeline 6361 -version 602
12331 grep FahCore_a0.exe
and the resulting list of ALL of the PIDs gets sent to cpulimit, however, it only acts on the first. The first PID isn't the one that needs limiting. 6368 is, or rather, 6366, 6367, or 6369 would also work. 6365 is the spawning process, however that just hangs out to check for WU updates and the like, it doesn't handle any computations itself.
Any ideas?
Thanks.
Re: HOWTO: Set maximum CPU consumption in percentage by any process
Hi,
I didn't know you have multiple process you would like to limit CPU...
You need to add some loop like for loop to cpu limit all pids.
Try something like:
PIDS=$(ps -eo pid | grep "process name you would like to CPU limit" | grep -v grep | gawk '{print $1}')
for i in $PIDS
do
cpulimit -p $i -l 5 -z &
done
Re: HOWTO: Set maximum CPU consumption in percentage by any process
Hey,
I don't really need to cpulimit on multiple PIDs, just one of the last 4 would work.
I'll try your script though, can't hurt to limit all of them.
Thank you very much for all of your help.
Re: HOWTO: Set maximum CPU consumption in percentage by any process
Hmm, didn't work. When I run it, nothing happens, no output in the term and the process isn't limited, no cpulimits in htop.
Here's the script:
Code:
#!/bin/bash
PIDS=$(ps -eo pid | grep "FahCore_a0.exe" | grep -v grep | gawk '{print $1}')
for i in $PIDS
do
cpulimit -p $i -l 5 -z &
done
Here's the command I run to limit it currently:
Code:
% sudo cpulimit -p 6368 -l 20
I only need to limit 1 of the 4 processes to limit the whole job I think.
Any ideas?
Thanks
Re: HOWTO: Set maximum CPU consumption in percentage by any process
Hi,
sorry I have forgotten to post "args" argument in ps command. Try this out (instead of PIDS line).
PIDS=$(ps -eo pid,args | grep "FahCore_a0.exe" | grep -v grep | gawk '{print $1}')
Regards
Re: HOWTO: Set maximum CPU consumption in percentage by any process
Spectacular. That works perfectly.
Thank you very much.
I keep telling myself that I need to know a shell scripting language, then I get swept up into something else.
This most recent diversion was Obj-C 2.0. I'll learn one eventually.
Re: HOWTO: Set maximum CPU consumption in percentage by any process
akoblentz,
thanks for letting me that this solution works for you.
Regards
Re: HOWTO: Set maximum CPU consumption in percentage by any process
Thanks, abcuser; this was quite comprehensive. The only addition I would make is to assure that gawk is installed:
sudo apt-get install gawk
I expect many users already have it, though it wasn't in my upgrade of Ubuntu 8.10 from 8.04. Thanks again!
Re: HOWTO: Set maximum CPU consumption in percentage by any process
benji66, yes I probably installed gawk before writting this how-to. Now I have updated how-to. Thanks for your note.
Re: HOWTO: Set maximum CPU consumption in percentage by any process
hi
would like ask is it possible to limit more processes with the above script you wrote for akoblentz (so not only one application)?
how would such script look like, or does it only need a grep extension?
other option:
to limit everything at anytime except a mysqld process (so basically add exemption)
much appreciated