#!/bin/sh
while [ true ]; do
#查询是否有8899正在运行的进程
netstat -an|grep 8899
if [ $? -ne 0 ]
then
nowtime=$(date +%Y-%m-%d\ %H:%M:%S)
echo "end process.....">> issuemonitor.bat
#check memory used rate
#|代表同时满足后面的信息 awk '{print $2}' 代表显示第二列 |sed -n '2p' 代表显示第2行
totalMemory=$(free -m|awk '{print $2}'|sed -n '2p')
##|代表过滤掉后面的信息 awk '{print $3}' 代表显示第三列 |sed -n '3p' 代表显示第3行
usedMemory=$(free -m|awk '{print $3}'|sed -n '3p')
freeMemory=$(free -m|awk '{print $4}'|sed -n '3p')
usedPerMemory=$(awk 'BEGIN{printf "%.0f",('$usedMemory'/'$totalMemory')*100}')
freePerMemory=$(awk 'BEGIN{printf "%.0f",('$freeMemory'/'$totalMemory')*100}')
minfo="totalMemory:$totalMemory MB,used:$usedMemory MB,free:$freeMemory MB,usedPercent:$usedPerMemory%,freePrecent:$freePerMemory%"
echo "$nowtime $minfo Memory used" >> issuemonitor.bat
#check cpu usedrate
top -m >> issuemonitor.bat
break
else
nowtime=$(date +%Y-%m-%d\ %H:%M:%S)
echo $nowtime >> issuemonitor.bat
echo "runing....." >> issuemonitor.bat
#代表8899下的所有的进程号 grep -v 是反向查找的意思 grep -v "$0" 代表不包含第一行
ID=`lsof -i:8899 | grep -v "$0"|grep -v "PID" | grep -v "grep" | awk '{print $2}'`
for id in $ID
do
jstack $id >> issuemonitor.bat
done
fi
sleep 5
done