I have some binary I want to run in terminal, but if I run it, it quickly starts to use 100% of both CPUs, 100% of memory and the whole Linux freeze in a few seconds. Also I don't have source (in order to calm it down right from the inside). I googled a lot and found and tried two possible solutions:
nice
- does literally nothing, the binary still uses 100% of both CPUs and 100% of memory quickly after I run itulimit -v
makes binary and even some other commands (like git) crash and throw some error
Is there any easy solution to limit CPU usage or memory by this binary and let it use e.g. only one CPU or 50% of both CPUs or only some small amount memory? I imagine something like this:
user@linux:~$ magical_command --limitmemory 1M --command mybinary
or:
user@linux:~$ magical_command --cpu both --limitcpu 50% --command mybinary
or:
user@linux:~$ magical_command --cpu one --limitcpu 100% --command mybinary
Update: From the comments below it looks like it is caused by memory rather than CPU overload, therefore I edited the question, but I still recommend @dbkeys' answer for those looking for CPU limiting magical command.
Update 2: As requested in the comments below: if I restart my notebook and only run terminal and that binary from terminal, nothing else, my notebook is significantly slowed down, mouse and keyboard works with delays or jerkily. But the binary is working for hours (most of time downloading and compiling dependencies), therefore I open Chrome and do some other stuff. If I open Chrome and let the binary run in the background, the whole linux freezes at all and nothing, even not mouse responds. I have to push a switch-off buton for 5 seconds to hard shutdown. But I can't just run it and go away, I want to work on some other stuff, therefore I want to limit somehow maybe both, that binary and Chrome.
taskset, might be the 'magical_command' you are looking for. According to its man page:
"...taskset is used to set or retrieve the CPU affinity of a running process given its pid, or to launch a new command with a given CPU affinity. CPU affinity is a scheduler property that "bonds" a process to a given set of CPUs on the system. The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs. "
This would limit mybinary to run on the first two cores:
Unfortunately, I don't see a parameter in taskset that would also limit what % of the CPU's time you want to allot it to run mybinary.