shell指令碼抓取問題進程

來源:互聯網
上載者:User

標籤:監控 shell 進程 load cpu

引用原文shell指令碼:
#/bin/bash
LOAD=$(awk ‘{print $1}‘ /proc/loadavg)
CPUNUM=$(grep -c processor /proc/cpuinfo)

if [ $(echo "$LOAD > $CPUNUM" | bc) = 1 ]; then
    RESULT=$(ps -eo pcpu,pmem,user,args | awk ‘$1 > 0‘ | sort -nr)
    if [ -n "$RESULT" ]; then
        echo "$RESULT" > /var/log/ps.$(date +"%Y%m%d%H%M")
    fi
fi

shell指令碼抓取問題進程
晚上某個時段收到某台伺服器load很高,但再登入伺服器查看時候load可能已經下降了,
所以使用shell指令碼來處理解決問題 監控頻率30秒1次 將系統負載超過一定數值就記錄到文本裡,
我這邊修改了原文shell指令碼

cat load.sh #!/bin/bash#--------------------------------------------------#Author:jimmygong#Email:[email protected]#FileName:load.sh#Function: #Version:1.0 #Created:2015-06-02#--------------------------------------------------sleeptime=30logpath="/opt/scripts"[[ -d $logpath ]] || mkdir $logpathechosucc () {    succstatus="[ Ok ]"    printf "\033[32m $succstatus $* \033[0m\n"}function usage (){    echo "Usage: $0 {start|stop}"}if [[ $# -ne 1 ]]then    usage    exit 1fifunction start () {    echosucc "Starting load monitor"    while true    do        load=$(cat /proc/loadavg|awk ‘{print $1}‘)        cpunum=$(cat /proc/cpuinfo|grep -c processor)        if [[ $(echo "$load > $cpunum" | bc) = 1 ]]        then            result=$(ps -eo pcpu,pmem,user,args|awk ‘$1 > 0‘ |sort -nr)            if [[ -n "$result" ]]            then                echo "$result" > $logpath/load.$(date +"%Y%m%d%H%M")            fi        fi        sleep $sleeptime    done}function stop () {    echo $$ > "$logpath/pidfile"    for PID in $(ps -eo pid,command|grep "$0"|grep -v grep|awk ‘{print $1}‘);    do        [[ $PID != $(cat $logpath/pidfile) ]] && kill -s TERM $PID >& /dev/null    done    echosucc ‘Stopping load monitor‘}case $1 in    start)    start &    ;;    stop)    stop    ;;    *)    usage    ;;esac

==========================參數說明==============================
man ps 可以看到下面說明
CODE   NORMAL   HEADER
%C     pcpu     %CPU
%G     group    GROUP
%P     ppid     PPID
%U     user     USER
%a     args     COMMAND
%c     comm     COMMAND
%g     rgroup   RGROUP
%n     nice     NI
%p     pid      PID
%r     pgid     PGID
%t     etime    ELAPSED
%u     ruser    RUSER
%x     time     TIME
%y     tty      TTY
%z     vsz      VSZ

本文出自 “7928217” 部落格,請務必保留此出處http://7938217.blog.51cto.com/7928217/1657964

shell指令碼抓取問題進程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.