Saturday, July 25, 2015

How to limit CPU Usage in Centos 6

Hello, it's been a while since my last post, today i want to share about limiting CPU Usage, this tutorial is not to important if you have VPS with high memory and fast processeor, but how about if you only have 384 MB memory and you have plenty application running in same time???, i suggest dont do that :D, but some of you maybe has limited budget or something else, i take an example if your VPS running wine, and of course running windows application on it, but your platform still Centos, dont miss understand, this tutorial is for centos, not windows family, let's say you running some autosurf program in your VPS, like hitleap, otohits, etc, it need a lot of resource of course, for a while, the apps will running with no problem, but when CPU Load is getting higher and higher, it will kill the apps by itself.

how to limit process on centos

Installation

Auto install will include Installing EPEL for Centos 6, CPULimit, and Screen.
Type below to begin auto install line by line

wget https://raw.githubusercontent.com/zaq111/bash/master/angelimus-install-cpulimit-screen.sh

angelimus-install-cpulimit-screen.sh

Done, now you already install CPULimit.

Usage

How to use cpulimit, let assume you want to run firefox, after run firefox, now we type top, to see how much firefox using CPU, see below.

top - 08:11:55 up 33 min,  3 users,  load average: 0.49, 0.20, 0.08
Tasks:  52 total,   2 running,  50 sleeping,   0 stopped,   0 zombie
Cpu(s): 65.4%us, 34.6%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    393216k total,   381648k used,    11568k free,        0k buffers
Swap:    98304k total,    13916k used,    84388k free,   259984k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1077 root      20   0  512m  85m  33m R 23.5 22.4   0:01.50 firefox
  701 root      20   0 39464  22m 4548 S  0.7  5.8   0:00.31 Xvnc
  848 root      20   0  199m  18m  13m S  0.7  4.8   0:06.75 knotify4
  875 root       9 -11 87240 3304 2636 S  0.7  0.8   0:01.75 pulseaudio

Firefox consume 23.5% and your total CPU Usage is 65.4%
Now we running cpulimit first by type like below

cpulimit -l 10 --exe=firefox

you will see "No process found" repeated all times, it's means firefox not running, just try running firefox, you will see the diffrent

CPULimit Running

[[email protected] ~]# cpulimit -l 10 --exe=firefox
No process found
No process found
No process found
Process 1223 found

See procees on "top"

top - 08:22:00 up 43 min,  4 users,  load average: 0.00, 0.03, 0.03
Tasks:  56 total,   3 running,  52 sleeping,   0 stopped,   1 zombie
Cpu(s): 44.4%us, 19.4%sy,  0.0%ni, 36.1%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    393216k total,   332660k used,    60556k free,        0k buffers
Swap:    98304k total,    14064k used,    84240k free,   257336k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1223 root      20   0  195m  25m  15m R  9.7  6.6   0:00.13 firefox
  848 root      20   0  199m  18m  13m S  2.2  4.8   0:17.95 knotify4
  875 root       9 -11 87240 3304 2636 S  0.7  0.8   0:04.13 pulseaudio
    1 root      20   0  2900 1368 1232 S  0.0  0.3   0:00.03 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd/93209

Now see firefox process, it's only using 9.7% - 10%, and total CPU Usage is only 44.4%

But remember, when you close your terminal that running cpulimit, your firefox process will running like before which is 23.5% and going higher, so how to make cpulimit still running on background??

Using Screen

Using screen, you can make process running on background, here some example of use

screen cpulimit -l 10 --exe=firefox

to detach screen, press Ctrl  + A D
to resume screen type screen -r
if you have more than 1 screen, type screen -list

[[email protected] ~]# screen -list
There is a screen on:
        1291.pts-2.don19        (Detached)

to resume screen in list just type screen -x 1296.pts-2.don19

That's all, just leave comment if you find some trouble with cpulimit.
Thanks for reading my tutorial, see you in next tutorial.

0 comments:

Post a Comment