Shell script Crawl problem process

Source: Internet
Author: User

To reference the original shell script:
#/bin/bash
load=$ (awk ' {print '} '/proc/loadavg)
cpunum=$ (grep-c processor/proc/cpuinfo)

If [$ (echo $LOAD > $CPUNUM "| bc) = 1]; Then
result=$ (Ps-eo Pcpu,pmem,user,args | awk ' $ > 0 ' | sort-nr)
If [-N "$RESULT"]; Then
echo "$RESULT" >/var/log/ps.$ (date + "%y%m%d%h%m")
Fi
Fi

Shell script Crawl problem process
At some point in the evening you receive a server with a high load, but when you log in to the server, the load may have dropped.
So use shell scripts to handle problem resolution monitoring frequency 30 seconds 1 times the system load exceeds a certain value is recorded in the text,
I modified the original shell script on my side.

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  "$" |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

========================== parameter Description ==============================
Man PS can see the following description
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

This article is from the "7928217" blog, please be sure to keep this source http://7938217.blog.51cto.com/7928217/1657964

Shell script Crawl problem process

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.