MESSAGE
DATE | 2017-01-31 |
FROM | Ruben Safir
|
SUBJECT | Subject: [Hangout-NYLXS] cpu limits
|
https://www.ostechnix.com/how-to-limit-cpu-usage-of-a-process-in-linux/
How To Limit CPU Usage Of A Process In Linux
by sk · January 28, 2017
Limit CPU Usage Of A Process In LinuxLimit CPU Usage Of A Process In Linux
Some times, a particular might consume more CPU usage, and affects the
performance of the whole system. Since that particular process consumes
more CPU, you couldn’t do other tasks. You need to wait for the
particular process to finish before starting a new task. Luckily, this
can be now solved by an useful utility called CPULimit. As the name
implies, CPULimit limits the CPU usage of a process. This can be really
useful to control the batch jobs, when you don’t want them to consume
more CPU usage.
Download – Free Guide: “How To Install and Use PowerShell on Linux”
The main goal of CPULimit is prevent a process from running for more
than a specified time ratio. It does not change the nice value or other
scheduling priority settings, but the real CPU usage. Also, it is able
to adapt itself to the overall system load, dynamically and quickly. It
will work on most Unix-like distributions. In this brief guide, we will
see how to limit the CPU usage of a process in Linux.
Limit CPU Usage Of A Process In Linux using CPULimit
Install CPULimit
CPULimit is available on most Unix-like distribution’s default
repositories. You can install it using the default package managers in
the respective Linux distribution as shown below.
On Arch Linux and its derivatives:
sudo pacman -S cpulimit
On Debian, Ubuntu, Linux Mint:
sudo apt-get install cpulimit
On RHEL, CentOS, Fedora:
Enable EPEL repository first.
sudo yum install epel-release
And then, install cpuclimit using command:
sudo yum install cpulimit
Or,
sudo dnf install cpulimit
On SUSE/openSUSE:
sudo zypper install cpulimit
Usage
Let us run a program that consumes more CPU usage. The following
commands should be run as root user.
Create a file called highcpu.sh.
vi highcpu.sh
Add the following contents.
#!/bin/bash
while :; do :; done;
Save and close the file. This short program will loop endlessly and
consumes maximum CPU usage. So, I recommend you to test it in a virtual
machine.
Make this file executable:
chmod +x highcpu.sh
Then, run the process in the background using command:
./highcpu.sh &
Sample output:
[1] 2331
Here 2331 is PID of the above process.
To view how much CPU it consumes, use “top” command.
top
Sample output:
top - 17:16:34 up 10 min, 2 users, load average: 1.12, 0.53, 0.26
Tasks: 87 total, 4 running, 83 sleeping, 0 stopped, 0 zombie
%Cpu(s): 91.9 us, 2.7 sy, 5.4 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 630108 total, 274868 free, 85904 used, 269336 buff/cache
KiB Swap: 1343484 total, 1343484 free, 0 used. 428172 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2331 root 20 0 113120 1184 1004 R 97.7 0.2 2:40.91 highcpu.sh
2412 root 39 19 244516 11020 4496 R 1.3 1.7 0:00.11 dnf
10 root 20 0 0 0 0 S 0.3 0.0 0:00.68 rcu_sched
1 root 20 0 43900 6480 3900 S 0.0 1.0 0:01.61 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.06 ksoftirqd/0
6 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/u2:0
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/0
11 root 20 0 0 0 0 R 0.0 0.0 0:00.38 rcuos/0
12 root rt 0 0 0 0 S 0.0 0.0 0:00.02 watchdog/0
13 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 khelper
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 perf
17 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 writeb
As you see in the above output, highcpu.sh process consumes more than
97% CPU usage. Since it consumed more CPU usage, It is quite difficult
to run other tasks. After a few minutes, you system might hang or
freeze. This is where CPULimt comes in help/
Now, let us limit the CPU usage of this process using CPULimit tool. Say
for example, we will limit this process by PID to 30% CPU.
To do so, run:
cpulimit -l 30 -p 2331 &
Here,
“-l 30” flag limits the process to 30%.
“-p 2331” is the PID of highcpu.sh
Now, let us again check the CPU usage of the above process using command:
top
Sample output:
top - 17:29:16 up 5 min, 1 user, load average: 0.61, 0.57, 0.27
Tasks: 86 total, 2 running, 83 sleeping, 1 stopped, 0 zombie
%Cpu(s): 7.2 us, 0.0 sy, 0.0 ni, 92.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 630108 total, 435348 free, 78052 used, 116708 buff/cache
KiB Swap: 1343484 total, 1343484 free, 0 used. 442040 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2331 root 20 0 113120 1180 1004 T 29.7 0.2 3:36.97 highcpu.sh
2334 root 9 -11 4324 692 572 S 1.7 0.1 0:00.47 cpulimit
1 root 20 0 43900 6480 3900 S 0.3 1.0 0:01.65 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.02 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
6 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kworker/u2:0
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/0
10 root 20 0 0 0 0 S 0.0 0.0 0:00.44 rcu_sched
11 root 20 0 0 0 0 R 0.0 0.0 0:00.37 rcuos/0
12 root rt 0 0 0 0 S 0.0 0.0 0:00.05 watchdog/0
13 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 khelper
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 perf
As you see in the above output, the CPU usage of highcpu.sh has been
dropped to 29.7%, which is very close to 30&%. Now, you can have more
CPU resources to run other processes freely.
We have seen how to limit a process by PID. Alternatively, we can run
the above command by specifying the name of the executable program file.
For example, the above command would be:
cpulimit -l 30 ./highcpu.sh &
Similarly, You can start any number processes in the background and
limit their CPU usage as you wish.
You can bring the processes which are running in the background to
foreground at any time using “fg” command:
fg
Sample output:
cpulimit -l 30 -p 2331
Conclusion
CPULimit can be useful while you are running a process that consumes
more CPU usage. Next time If you notice a program consumes more CPU
usage, just find PID of the process using “top” command and limit its
CPU usage to a minimum value using CPULimit command as described above.
You can also use this application to test how an application performs
under low CPU usage.
That’s all for now. I will be soon here with another interesting and
useful guide. if you find this guide useful, share it on your social
networks and support OSTechNix.
Cheers!
Resource:
CPULimit on GitHub
--
So many immigrant groups have swept through our town
that Brooklyn, like Atlantis, reaches mythological
proportions in the mind of the world - RI Safir 1998
http://www.mrbrklyn.com
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
http://www.nylxs.com - Leadership Development in Free Software
http://www2.mrbrklyn.com/resources - Unpublished Archive
http://www.coinhangout.com - coins!
http://www.brooklyn-living.com
Being so tracked is for FARM ANIMALS and and extermination camps,
but incompatible with living as a free human being. -RI Safir 2013
--
So many immigrant groups have swept through our town
that Brooklyn, like Atlantis, reaches mythological
proportions in the mind of the world - RI Safir 1998
http://www.mrbrklyn.com
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
http://www.nylxs.com - Leadership Development in Free Software
http://www2.mrbrklyn.com/resources - Unpublished Archive
http://www.coinhangout.com - coins!
http://www.brooklyn-living.com
Being so tracked is for FARM ANIMALS and and extermination camps,
but incompatible with living as a free human being. -RI Safir 2013
_______________________________________________
hangout mailing list
hangout-at-nylxs.com
http://www.nylxs.com/
|
|