I have a bunch of processes of the same name - they take up 100% of my CPU time, I want this to be no more than 50%.
How can I do this without installing any extra software?
For a kernel-level mechanism for this, you need to read up on Linux Control Groups. This is an area still in active development, so you'll need to ensure that what you read is consistent with the kernel you have (or are willing to install).
Although you said not to install anything additional, you might consider installing cgred
to automate moving processes with matching names to your CPU cgroup. On Debian, this in in the cgroup-bin
package.
You can do so with bash shell, sleep, pgrep, and pkill commands using STOP and CONT signals.
The following one-liner will make the processes use maximum of 50% of total availabile CPU time. It's going to run for each 100 miliseconds, then it'll be stopped for another 100 milliseconds.
while [ True ]; do pkill -STOP processname; sleep 0.1s; pkill -CONT processname; sleep 0.1s; done
Here's the same code split into multiple lines for readability:
while [ True ]
do pkill -STOP processname
sleep 0.1s
pkill -CONT processname
sleep 0.1s
done
No mater how much processes exist maching the processname pattern, they can't excess 50% of CPU usage, becasue they are all stopped and continued at the same time - which means they need to share the avaiabile CPU time between them, when they are active. This ensures the limit is met.
There is a program called cpulimit that does roughly the same thing.
Without installing any extra software you have nice
. The value range is from -20 to 19.
nice -20 some_command
gives some_command
the highest priority, and
nice 19 some_command
gives some_command
the lowest priority.
nice
in this context surely is worth the effort.
as written above could be used as a script to assist OOM-killer Or prevent its use as well
say when cpu overall hits 75% for x mins run pkill.sh
../scripts/boottime/OOMkiller.sh
while [ true] ;
do inxi -tcm5 > ~/cpu.log
sleep 0.5s
inxi -tcm5 >> ~/cpu/log | diff -m > ~/offender.log
done
for i in offender.log ;
do ./scripts/pkill.sh
done
../scripts/pkill.sh
while [true]
do pgrep $(i) -STOP $PID
pkill -STOP $PID
sleep 0.5s
pkill -CONT $PID
done
nice
...