I have a bash script which runs other cpu-intensive commands .
When I apply cpulimit
on the bash script, the output of top
shows processes for the commands inside the script still run without limitation by cpulimit
. I wonder how I can limit the cpu usage of the commands inside the bash script?
Best Answer
According to
cpulimit --help
:I have not tested whether this applies to children of children, nor looked into how this is implemented.
Alternatively, you could use
cgroups
, which is a kernel feature.Cgroups don't natively provide a means to limit child processes as well, but you can use the cg rules engine daemon (cgred) provided by libcgroup; the
cgexec
andcgclassify
commands that come from the libcgroup package provide a--sticky
flag to make rules apply to child processes as well.Be aware that there is a race condition involved which could (at least theoretically) result in some child processes not being restricted correctly. However, as you're currently using
cpulimit
which runs in userspace anyway, you already don't have 100% reliable CPU limitations so this race condition shouldn't be a deal-breaker for you.I wrote rather extensively about the cg rules engine daemon in my self-answer here: