4

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?

3
  • Do you mean to limit each process to 50% of a CPU, or do you want to limit the aggregate CPU usage of all those processes? The answer may well depend on that distinction. May 29 '15 at 9:41
  • Have you considered setting process affinity for them to 50% of cores?
    – PTwr
    May 29 '15 at 13:06
  • 2
    Most of the time I see questions like this, I wonder if the OP wouldn't be better of using nice...
    – Axel
    May 29 '15 at 14:07
4

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.

3

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.

2
  • 1
    The downside to this is that it plays poorly in the idle state, because you are now waking 10 times a second whether it's necessary or not. This will diminish the benefits of NOHZ in your kernel. Of course, if your system is never idle, that won't be an issue for you, but it is worth at least mentioning. See my answer for an alternative. May 29 '15 at 9:31
  • 1
    Processes sometimes react weirdly if stopped and continued. Especially if they are terminal-interactive. So this might not work with every program, and a corresponding warning should be included in the answer.
    – Alfe
    May 29 '15 at 12:26
2

Without installing any extra software you have nice. The value range is from -20 to 19.

nice -20 some_command

gives some_commandthe highest priority, and

nice 19 some_command

gives some_commandthe lowest priority.

4
  • If you nice a bunch of processes, they won't get 50% of CPU time when something else wants to use 100% of the CPU. May 29 '15 at 11:43
  • 2
    … And if nothing else is using CPU time, they still are gobbling up all the 100%. But mentioning nice in this context surely is worth the effort.
    – Alfe
    May 29 '15 at 12:27
  • 1
    Why would anyone want a process to get 50% of CPU in the first place? I cannot think of any non-esoteric scenario where this would make more sense than using nice.
    – Axel
    May 29 '15 at 14:11
  • 1
    Doesn't seem like an esoteric scenario to me as I encounter it frequently. Users wanting to run larger processing tasks, but not allow them to knock down the rest of the machine at the same time. A % of CPU limit would be ideal. May 29 '15 at 15:09
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.