SUSE LINUX Enterprise & openSUSE Community
มกราคม 18, 2019, 10:39:54 PM *
ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน
ส่งอีเมล์ยืนยันการใช้งาน?

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น
ข่าว:
 
   หน้าแรก   ช่วยเหลือ ค้นหา เข้าสู่ระบบ สมัครสมาชิก  

[Why we need your support] SUSE and openSUSE are trademarks of Attachmate Group, Inc. - WE ARE NOT IN ANY WAY ASSOCIATED WITH SUSE AND ATTACHMATE GROUP. SUSEThailand.com is a SUSE Linux user and community found in Thailand but not limited to other country suse linux user to join in. Currently active contents (How to's, Scripts, Tips, Tricks, Tutorials, Linux Command Line, and Troubleshooting) this suse linux how to's and expert support are SUSE Linux.
หน้า: [1]   ลงล่าง
  พิมพ์  
ผู้เขียน
หัวข้อ: การติดตั้งโปรแกรม cpulimit  (อ่าน 1658 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
Sontaya
Administrator
Expert : ผู้เชี่ยวชาญ
*****

Karma: +1/-0
ออฟไลน์ ออฟไลน์

กระทู้: 1931


Administrator


เว็บไซต์
« เมื่อ: มกราคม 28, 2012, 11:11:50 AM »


การติดตั้งโปรแกรม cpulimit

1. ติดตั้งโปรแกรม cpulimit

ดาวน์โหลด: http://software.opensuse.org/search?q=cpulimit&baseproject=SUSE%3ASLE-11%3ASP1&lang=en&exclude_debug=true


2. สร้างไฟล์ cpulimt สคริปต์สำหรับการ limit processses หลายๆ PIDs

ตัวอย่างการ limit processes เฉพาะโปรเซสที่ชื่อ "plugin-container|npviewer.bin|acroread|chromium"

โค๊ด:
vi cpulimit_daemon.sh

โค๊ด:
#!/bin/bash
# ==============================================================
# CPU limit daemon - set PID's max. percentage CPU consumptions
# ==============================================================

# Variables
CPU_LIMIT=30       # Maximum percentage CPU consumption by each PID
DAEMON_INTERVAL=3   # Daemon check interval in seconds
BLACK_PROCESSES_LIST="plugin-container|npviewer.bin|acroread|chromium"   # Limit only processes defined in this variable. If variable is empty (default) all violating processes are limited.
WHITE_PROCESSES_LIST=   # Limit all processes except processes defined in this variable. If variable is empty (default) all violating processes are limited.

# Check if one of the variables BLACK_PROCESSES_LIST or WHITE_PROCESSES_LIST is defined.
if [[ -n "$BLACK_PROCESSES_LIST" &&  -n "$WHITE_PROCESSES_LIST" ]] ; then    # If both variables are defined then error is produced.
   echo "At least one or both of the variables BLACK_PROCESSES_LIST or WHITE_PROCESSES_LIST must be empty."
   exit 1
elif [[ -n "$BLACK_PROCESSES_LIST" ]] ; then                                 # If this variable is non-empty then set NEW_PIDS_COMMAND variable to bellow command
   NEW_PIDS_COMMAND="top -b -n1 -c | grep -E '$BLACK_PROCESSES_LIST' | gawk '\$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"
elif [[ -n "$WHITE_PROCESSES_LIST" ]] ; then                                 # If this variable is non-empty then set NEW_PIDS_COMMAND variable to bellow command
   NEW_PIDS_COMMAND="top -b -n1 -c | gawk 'NR>6' | grep -E -v '$WHITE_PROCESSES_LIST' | gawk '\$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"
else
   NEW_PIDS_COMMAND="top -b -n1 -c | gawk 'NR>6 && \$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"
fi

# Search and limit violating PIDs
while sleep $DAEMON_INTERVAL
do
   NEW_PIDS=$(eval "$NEW_PIDS_COMMAND")                                                                    # Violating PIDs
   LIMITED_PIDS=$(ps -eo args | gawk '$1=="cpulimit" {print $3}')                                          # Already limited PIDs
   QUEUE_PIDS=$(comm -23 <(echo "$NEW_PIDS" | sort -u) <(echo "$LIMITED_PIDS" | sort -u) | grep -v '^$')   # PIDs in queue

   for i in $QUEUE_PIDS
   do
       cpulimit -p $i -l $CPU_LIMIT -z &   # Limit new violating processes
   done
done


โค๊ด:
mkdir /opt/cpulimit

cp cpulimit_daemon.sh /opt/cpulimit/

chmod 755 /opt/cpulimit/cpulimit_daemon.sh



3. สร้างไฟล์ cpulimit init สคริปต์

โค๊ด:
vi cpulimit

โค๊ด:
#!/bin/sh
#
# Author: Sontaya Potibut <susethailand.com@gmail.com>, Jan 28, 2012
#
# /etc/init.d/cpulimit
#
#   and symbolic its link
#
# /sbin/rccpulimit
#
# System startup script for the CPU limit daemon
#
### BEGIN INIT INFO
# Provides:            CPU Limit
# Default-Start:       2 3 5
# Default-Stop:        0 1 6
# Description:         CPU Limit
### END INIT INFO

# Source SuSE config, only if exists with size greater zero
test -s /etc/rc.config && \
    . /etc/rc.config

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status

CPULIMIT_BIN=/opt/cpulimit/cpulimit_daemon.sh

test -s /etc/rc.status && \
     . /etc/rc.status

test -x $CPULIMIT_BIN || exit 5

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

# change umask to avoid problems in wrong file permission of /etc/printcap
# (SuSE buzilla #16567)
umask 022

case "$1" in
    start)
echo -n "Starting cpulimit"
## Start daemon with startproc(8). If this fails
## the echo return value is set appropriate.

# NOTE: startproc return 0, even if service is
# already running to match LSB spec.
if [ "$PREVLEVEL" = "N" -a "$RUNLEVEL" = "5" ]; then
/usr/bin/ionice -c 3 startproc $CPULIMIT_BIN
else
startproc $CPULIMIT_BIN > /var/log/cpulimit.log 2>&1 &
fi

# Remember status and be verbose
rc_status -v
;;
    stop)
echo -n "Shutting down cpulimit"
## Stop daemon with killproc(8) and if this fails
## set echo the echo return value.

killproc -TERM $CPULIMIT_BIN
        ps aux| grep "cpulimit_daemon"|sort -rn |awk 'NR>1 {print $2}' | xargs -r kill -9
        pkill cpulimit
# Remember status and be verbose
rc_status -v
;;
    try-restart)
## Stop the service and if this succeeds (i.e. the
## service was running before), start it again.
## Note: try-restart is not (yet) part of LSB (as of 0.7.5)
$0 status >/dev/null &&  $0 restart

# Remember status and be quiet
rc_status
;;
    restart)
## Stop the service and regardless of whether it was
## running or not, start it again.
$0 stop
$0 start

# Remember status and be quiet
rc_status
;;
    force-reload)
## Signal the daemon to reload its config. Most daemons
## do this on signal 1 (SIGHUP).
## If it does not support it, restart.

if ps -C cpulimit -o user | grep -q '^root$'; then
   echo -n "Reload service cpulimit"
   killproc -HUP $CPULIMIT_BIN
   rc_status -v
else
   $0 restart
fi
;;
    reload)
## Like force-reload, but if daemon does not support
## signalling, do nothing (!)

# If it supports signalling:
if ps -C cpulimit -o user | grep -q '^root$'; then
   echo -n "Reload service cpulimit"
   killproc -HUP $CPULIMIT_BIN
   rc_status -v
else
   echo -n '"reload" not possible in RunAsUser mode - use "restart" instead'
   rc_status -s
fi
;;
    status)
echo -n "Checking for CPU limit: "
## Check status with checkproc(8), if process is running
## checkproc will return with exit status 0.

# Status has a slightly different for the status command:
# 0 - service running
# 1 - service dead, but /var/run/  pid  file exists
# 2 - service dead, but /var/lock/ lock file exists
# 3 - service not running

# NOTE: checkproc returns LSB compliant status values.
checkproc $CPULIMIT_BIN
rc_status -v
;;
    probe)
## Optional: Probe for the necessity of a reload,
## give out the argument which is required for a reload.

rc_failed 3
;;
    *)
echo "Usage: $0 {start|stop|status}"
exit 1
;;
esac
rc_exit



โค๊ด:
cp cpulimit /etc/init.d/

chmod 755 /etc/init.d/cpulimit


- กำหนด runlevel boot-up script

โค๊ด:
yast runlevel

คลิกเลือก Server "cpulimit" แล้วคลิก Enable the service

System Services (Runlevel) = 5


- ทำลิงค์

โค๊ด:
ln -s /etc/init.d/cpulimit /usr/sbin/rccpulimit


- การใช้งานสคริปต์

โค๊ด:
rccpulimit start

rccpulimit status

rccpulimit stop

Download CPU limit script
« แก้ไขครั้งสุดท้าย: พฤศจิกายน 06, 2012, 02:49:12 PM โดย Sontaya » แจ้งลบกระทู้นี้หรือติดต่อผู้ดูแล   บันทึกการเข้า

ageLOC Technology
หน้า: [1]   ขึ้นบน
  พิมพ์  
 
กระโดดไป:  

(@)2007 SUSE Linux user community found in Thailand. This site is not an official openSUSE and SUSE website, and is not in any way affiliated with or endorsed by SUSE Linux GmbH or Novell. openSUSE and SUSE are trademarks of Novell, Inc. in the United States and other countries.
Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!
หน้านี้ถูกสร้างขึ้นภายในเวลา 0.034 วินาที กับ 20 คำสั่ง (Pretty URLs adds 0.007s, 2q)