几个 简单的shell 脚本 借鉴一下

第一个 定时抓取服务器JVM相关信息

#! /bin/sh
    
#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
    
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

这个可以跑个定时器:

crontab -e

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

第二个脚本:对上面的脚本改进一下,只有系统负载达到指定的值之后才打印JVM信息

Java代码 复制代码 收藏代码
  1. #! /bin/sh 
  2.  
  3.  
  4. function printJVMMessage() { 
  5.  
  6. #get_current_time 
  7. export JAVA_HOME=/opt/jdk1.6.0_10/ 
  8. export PATH=$JAVA_HOME/bin:$PATH 
  9. export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH 
  10.  
  11. #only the system load greate than 1*8PU then start to print the gc details 
  12.  
  13.  
  14. date=`date "+%Y-%m-%d-%H:%M:%S"
  15.  
  16. Root="/data/dxm/" 
  17.  
  18.  
  19. jmapends=$Root$date"_jmap.txt" 
  20. jstackends=$Root$date"_jstack.txt" 
  21. jstateends=$Root$date"_jstate.txt" 
  22.  
  23.  
  24. #start to register the jmap message 
  25. echo "start to register the jmap to "$jmapends 
  26. jmap -histo:live `jps|grep Resin |awk 'NR==1'|awk '{print $1}'` > $jmapends 
  27.  
  28. sleep 2 
  29.  
  30. #start to register the jtack message 
  31. echo "start to register the jstack to "$jstackends 
  32. jstack -l `jps|grep Resin |awk 'NR==1'|awk '{print $1}'` > $jstackends 
  33.  
  34. sleep 2 
  35.  
  36. #start to register the jstate message 
  37. echo "start to register the jstate message to "$jstateends 
  38. jstat -gcutil -t -h20 `jps|grep Resin |awk 'NR==1'|awk '{print $1}'1000 20 > $jstateends 
  39.  
  40.  
  41.  
  42. #System Max Load Per CPU,when the SystemLoad reached the MAX_LOAD * CPU_NUMBER then start register the jvm message 
  43. MAX_LOAD=1 
  44.  
  45. CPU_NUMBER=`cat /proc/cpuinfo | grep processor |wc -l` 
  46.  
  47. <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)) 
  48. #TOTAL_LOAD_TO_WARN=1 
  49.  
  50. CURRENT_SYSTEM_LOAD=`uptime | awk '{print $(NF-2)}' | sed 's/,//'
  51.  
  52. Start_Time=`date "+%Y-%m-%d-%H:%M:%S"
  53.  
  54. echo 'start to print the jvm message...currenttime is:'$Start_Time 
  55. echo 'current system load is'$CURRENT_SYSTEM_LOAD' and TOTAO_LOAD_TO_WARN is '$TOTAL_LOAD_TO_WARN 
  56.  
  57. if [ $(echo "$CURRENT_SYSTEM_LOAD > $TOTAL_LOAD_TO_WARN"|bc) == 1 ]; then 
  58.     echo 'printint the jvm message...' 
  59.     printJVMMessage 
  60. fi 
  61.  
  62. Finish_Time=`date "+%Y-%m-%d-%H:%M:%S"
  63. echo 'finish to print the jvm message...and current time is:'$Finish_Time 
#! /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`

TOTAL_LOAD_TO_WARN=$(($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

第三个脚本:比较两个版本中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=$1
LibPath2=$2
date=`date "+%Y-%m-%d-%H:%M:%S"`
    
LOG=$date"_log.txt"
echo "-----------log file " $LOG 
    
echo LibPath1: $LibPath1
echo LibPath2: $LibPath2
echo LibPath1: $LibPath1 \n >> $LOG
echo LibPath2: $LibPath2 \n >> $LOG
echo >> $LOG
echo >> $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]]]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值