您的位置:首页 > 运维架构 > Shell

shell 监控一个程序或者命令执行时间

2013-08-07 15:10 531 查看
这个需求是我一个“朋友”问我的,她想监控系统上一个命令,如果执行超过5分钟还没有结束,就要kill掉或者发邮件,短信之类的!
我最开始的思路比较简单:

#!/bin/bash
#author junun
#blog http://angus717.blog.51cto.com function angus() {
timestart_array=(`ps aux | grep test.sh | grep -v "grep" | awk '{print $2"_"$9}'`)
timenow=`date -d "3 minute ago" | awk '{print $4}'  | cut -d":" -f1-2`
declare -a arror_time=($@)
for i in `echo ${arror_time[*]}`;do
m=`echo $i | cut -d"_" -f1`
echo $m
n=`echo $i | cut -d"_" -f2`
echo $n
if [[ $n == $timenow ]];then
echo "$m will be killed"
kill $m
#mail -s "The process id ($m)is running time too long!" 123456@qq.com
else
echo "this ok"
fi
done
}
angus
就是根据ps start那项判断该命令执行多久了(为了测试我写了个死循环test.sh),但是人家说了这个不行,他们老大说判断pid什么的,然后他老大写出来一个让他很自豪的脚本,然后很很鄙视了小生一把!好吧,水平不高咱就虚心请教呗!人家还怕咱学会了,然后不鸟咱了,人活一口气!
第二天果断修改脚本:
[root@localhost ~]# cat nimawohenshengqi.sh
#!/bin/bash
#author junun
#blog http://angus717.blog.51cto.com #
function checkdate() {

x=$1
y=$2
z=$3
nowtime=`/bin/date | awk '{print $2" "$3" "$4}'`
nowtime_day=`echo $nowtime | awk '{print $2}'`
nowtime_hour=`echo $nowtime | awk -F"[ |:]" '{print $3}'`
nowtime_minute=`echo $nowtime | awk -F"[ |:]" '{print $4}'`
if [ $x = $nowtime_day ];then
now_mins=`expr $nowtime_hour \* 60 + $nowtime_minute`
star_mins=`expr $y \* 60 + $z`
time_interval=`expr $now_mins - $star_mins`
else
echo "The process is running time too long!"
fi
}
function angus() {
runtime_interval=$1
[ -z "${runtime_interval:-}" ] && runtime_interval=5
timestart_array=(`ps aux | grep test.sh | grep -v "grep" | awk '{print $2}'`)
for i in `echo ${timestart_array[*]}`;do
m=`ls -ld  /proc/$i |  awk           '{print $7}'`
n=`ls -ld  /proc/$i |  awk -F"[ |:]" '{print $9}'`
a=`ls -ld  /proc/$i |  awk -F"[ |:]" '{print $10}'`
checkdate $m $n $a
if [[ $time_interval -ge $runtime_interval ]];then
echo "$i will be killed"
/bin/kill $i
else
echo "Command is ok"
fi
done
}
angus $1
修改过后可以接受参数定义(定义执行几分钟没有的程序或者命令会被kill)总的来说是可以实现功能了,但是我感觉有点复杂,请伙伴们,给出更优秀的方案!

本来都顶格的,但是代码贴上来都变了!!!!!!!!!
本文出自 “面对自己” 博客,请务必保留此出处http://angus717.blog.51cto.com/1593644/1266037
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐