欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

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

程序员文章站 2022-12-09 14:34:15
#!/bin/shwhile [ true ]; do #查询是否有8899正在运行的进程netstat -an|grep 8899if [ $? -ne 0 ]thennowtime=$(date +%Y-%m-%d\ %H:%M:%S)echo "end process.....">> issu ......

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