It happened to me multiple times now that a program which entered an infinite loop made the entire machine completely unresponsive, one time even foreign JavaScript, and I had to power off. This is unacceptable so I want to limit the CPU usage of processes. Using limits.conf seems obvious, but it says that the unit is max CPU time (MIN). But as far as I know, CPU time is the time the CPU spends doing things, and in this case the complete timeframe is unknown. A limit of 1 CPU minute would mean what? (On a sidenote, ulimit claims that the unit is seconds, for even more confusion...) So, how can I set stuff like "Don't ever allow any process to exceed to use more than half of the CPU at any given point in time"

  • 2
    There really isn't a reason to limit CPU. If you need to limit the amount of time any particular process is consuming, you can nice it to an acceptable level, but aside from that any process simply consumes a slice of time based on its scheduling priority. If you're determined that you need to limit CPU consumption, there is a tool called cpulimit available on github. – Fubar May 26 '20 at 17:08
  • 1
    what is happening to memory usage, at the time? – ctrl-alt-delor May 26 '20 at 17:17
  • @Fubar If a JavaScript for(;;){} can make my computer so unresponsive that I have to cut the power, I think there is a reason to limit CPU. @ctrl-alt-delor I don't know because at that point opening a Linux tty took 2 minutes, and I didn't bother to wait for bash starting + htop starting. – Uncreative Name May 28 '20 at 8:04
  • In the case of such a loop you've got an application issue, not a system issue. It's the applicatoin that needs fixing. Using nice on the process running the loop would keep it from being so disruptive, as normal processing would be able to take precedence for CPU timeslices. You could also try tuning the length of a timeslice to have them available more frequently. – Fubar May 28 '20 at 12:47
  • The problem is that it doesn't matter whether it is an application issue because my PC is fucked. Also the application is Firefox. And yes, I set Firefox to always start with a nice of 20, but I had the same problem other times (for example a VLC bug). – Uncreative Name May 31 '20 at 10:43

You can not do with ulimit, there is a tool that can do it, by suspending and un-suspending the process (cpulimit). It may be part of your OS, it is part of Debian, but not installed by default. There is also kernel namespaces. However you should not need to do this. Unix does not behave badly in this situation. You can also use nice however I have rarely seen a situation when it was needed. Your problem may have another cause, such as swapping.

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.