幾個 簡單的shell 指令碼 借鑒一下

來源:互聯網
上載者:User

第一個 定時抓取伺服器JVM相關資訊

#! /bin/sh    #get_current_timeexport JAVA_HOME=/opt/jdk1.6.0_10/export PATH=$JAVA_HOME/bin:$PATHexport CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH    date=`date "+%Y-%m-%d-%H:%M:%S"`    Root="/data/dxm/"        jmapends=$Root$date"_jmap.txt"jstackends=$Root$date"_jstack.txt"jstateends=$Root$date"_jstate.txt"                    #start to register the jmap messageecho "start to register the jmap to "$jmapendsjmap -histo:live `jps|grep Resin |awk 'NR==1'|awk '{print $1}'` > $jmapends    sleep 2    #start to register the jtack messageecho "start to register the jstack to "$jstackendsjstack -l `jps|grep Resin |awk 'NR==1'|awk '{print $1}'` > $jstackends    sleep 2    #start to register the jstate messageecho "start to register the jstate message to "$jstateendsjstat -gcutil -t -h20 `jps|grep Resin |awk 'NR==1'|awk '{print $1}'`  1000 20 > $jstateends

這個可以跑個定時器:

crontab -e

*/10 * * * * sh /timer/timer.sh  #每十分鐘抓一次

第二個指令碼:對上面的指令碼改進一下,只有系統負載達到指定的值之後才列印JVM資訊

Java代碼 #! /bin/sh      function printJVMMessage() {    #get_current_time  export JAVA_HOME=/opt/jdk1.6.0_10/  export PATH=$JAVA_HOME/bin:$PATH  export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH    #only the system load greate than 1*8PU then start to print the gc details      date=`date "+%Y-%m-%d-%H:%M:%S"`    Root="/data/dxm/"      jmapends=$Root$date"_jmap.txt"  jstackends=$Root$date"_jstack.txt"  jstateends=$Root$date"_jstate.txt"      #start to register the jmap message  echo "start to register the jmap to "$jmapends  jmap -histo:live `jps|grep Resin |awk 'NR==1'|awk '{print $1}'` > $jmapends    sleep 2    #start to register the jtack message  echo "start to register the jstack to "$jstackends  jstack -l `jps|grep Resin |awk 'NR==1'|awk '{print $1}'` > $jstackends    sleep 2    #start to register the jstate message  echo "start to register the jstate message to "$jstateends  jstat -gcutil -t -h20 `jps|grep Resin |awk 'NR==1'|awk '{print $1}'`  1000 20 > $jstateends    }      #System Max Load Per CPU,when the SystemLoad reached the MAX_LOAD * CPU_NUMBER then start register the jvm message  MAX_LOAD=1    CPU_NUMBER=`cat /proc/cpuinfo | grep processor |wc -l`    <strong style="background-color: rgb(255, 255, 255);"><span style="color: rgb(255, 0, 0);">TOTAL_LOAD_TO_WARN</span></strong>=$(($MAX_LOAD*$CPU_NUMBER))  #TOTAL_LOAD_TO_WARN=1    CURRENT_SYSTEM_LOAD=`uptime | awk '{print $(NF-2)}' | sed 's/,//'`    Start_Time=`date "+%Y-%m-%d-%H:%M:%S"`    echo 'start to print the jvm message...currenttime is:'$Start_Time  echo 'current system load is'$CURRENT_SYSTEM_LOAD' and TOTAO_LOAD_TO_WARN is '$TOTAL_LOAD_TO_WARN    if [ $(echo "$CURRENT_SYSTEM_LOAD > $TOTAL_LOAD_TO_WARN"|bc) == 1 ]; then      echo 'printint the jvm message...'      printJVMMessage  fi    Finish_Time=`date "+%Y-%m-%d-%H:%M:%S"`  echo 'finish to print the jvm message...and current time is:'$Finish_Time 

#! /bin/shfunction printJVMMessage() {#get_current_timeexport JAVA_HOME=/opt/jdk1.6.0_10/export PATH=$JAVA_HOME/bin:$PATHexport CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH#only the system load greate than 1*8PU then start to print the gc detailsdate=`date "+%Y-%m-%d-%H:%M:%S"`Root="/data/dxm/"jmapends=$Root$date"_jmap.txt"jstackends=$Root$date"_jstack.txt"jstateends=$Root$date"_jstate.txt"#start to register the jmap messageecho "start to register the jmap to "$jmapendsjmap -histo:live `jps|grep Resin |awk 'NR==1'|awk '{print $1}'` > $jmapendssleep 2#start to register the jtack messageecho "start to register the jstack to "$jstackendsjstack -l `jps|grep Resin |awk 'NR==1'|awk '{print $1}'` > $jstackendssleep 2#start to register the jstate messageecho "start to register the jstate message to "$jstateendsjstat -gcutil -t -h20 `jps|grep Resin |awk 'NR==1'|awk '{print $1}'`  1000 20 > $jstateends}#System Max Load Per CPU,when the SystemLoad reached the MAX_LOAD * CPU_NUMBER then start register the jvm messageMAX_LOAD=1CPU_NUMBER=`cat /proc/cpuinfo | grep processor |wc -l`TOTAL_LOAD_TO_WARN=$(($MAX_LOAD*$CPU_NUMBER))#TOTAL_LOAD_TO_WARN=1CURRENT_SYSTEM_LOAD=`uptime | awk '{print $(NF-2)}' | sed 's/,//'`Start_Time=`date "+%Y-%m-%d-%H:%M:%S"`echo 'start to print the jvm message...currenttime is:'$Start_Timeecho 'current system load is'$CURRENT_SYSTEM_LOAD' and TOTAO_LOAD_TO_WARN is '$TOTAL_LOAD_TO_WARNif [ $(echo "$CURRENT_SYSTEM_LOAD > $TOTAL_LOAD_TO_WARN"|bc) == 1 ]; thenecho 'printint the jvm message...'printJVMMessagefiFinish_Time=`date "+%Y-%m-%d-%H:%M:%S"`echo 'finish to print the jvm message...and current time is:'$Finish_Time

第三個指令碼:比較兩個版本中jar包是否相同(有改變):

#! /bin/sh    #compare the lib files's md5 int file1  with those in file2#if it only exist in file1 or one lib file's md5 is not equal i#with the other then print it to the log.txt     LibPath1=$1LibPath2=$2date=`date "+%Y-%m-%d-%H:%M:%S"`    LOG=$date"_log.txt"echo "-----------log file " $LOG     echo LibPath1: $LibPath1echo LibPath2: $LibPath2echo LibPath1: $LibPath1 \n >> $LOGecho LibPath2: $LibPath2 \n >> $LOGecho >> $LOGecho >> $LOG    for File in `ls -f $LibPath1|grep jar `;do      MD5=`md5sum $1$File |awk 'NR==1'|awk '{print $1}'`  File2=$LibPath2$File  #echo "File2-----------------" $File2        #if the same lib file exist  if [ ! -f "$File2" ]; then            echo $File only exist LibPath1        echo $File only exist LibPath1 \n >> $LOG        echo >> $LOG      #if the same file exist in directory2  else        MD52=`md5sum $File2 |awk 'NR==1'|awk '{print $1}'`        if [ "$MD5" != "$MD52" ]; then                echo the $File changed  \n >> $LOG                echo >> $LOG                echo the $File changed        fi      fi    done
 
 
上的那個檔案為
md5compare.sh
 
運行是可以用以下命令運行:
 
./md5compare.sh  libpath1 libpath2
注意libpath1,libpath2 必須以"/" 結尾比如:
/opt/ROOT_2011-09-08-09:57:26/WEB-INF/lib/
而不是
/opt/ROOT_2011-09-08-09:57:26/WEB-INF/lib]]]
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.