当前位置: 移动技术网 > 科技>操作系统>Linux > lunix脚本进程挂掉时显示cpu和内存信息及挂掉的时间

lunix脚本进程挂掉时显示cpu和内存信息及挂掉的时间

2019年04月30日  | 移动技术网科技  | 我要评论

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网