应同事要求,写了个shell,主要功能为查找超时的进程,并关闭!
调用方式:
sh monter_shell_minute.sh shell_sleep 30
shell_sheep :? 为进程名
30 : 为30分钟
从打印的日志能看出会多两个PID,不要惊慌,由于你执行时会带有 搜索的“程序名”,且执行时也会产生一个,所以会有两个pid(运行时间比较短)。
也可以在crontab 中写任务,每隔一段时间执行一次。。
?
#!/bin/bash
# author : jackical
input_program=$1
input_minue=$2
sn_lines=`ps -ef|grep ${input_program}|grep -v grep|awk ‘{print $2}‘`
echo ‘sn_lines:‘
echo ${sn_lines}
if [ "${sn_lines}" -eq "" ]
then
echo program has end!
else
IFS=$‘n‘
for i in ${sn_lines};
do
echo "line pid:" ${i}
run_info=`ps -eo pid,etime|grep ${i}|awk ‘{print $2}‘`
run_time=${run_info/${i}/}
run_time2=${run_time/[[:space:]]/} #程序运行时间
echo "program run time:" ${run_time2}
echo "run_time2 length :" ${#run_time2}
# 超过一天
if [ ${#run_time2} -gt 9 -a $input_minue -lt 86400 ]
then
echo "已超时!"
cmd="kill -9 "$i
eval $cmd
echo $cmd
else
if [ ${#run_time2} -gt 9 ]
then
echo "big 10"
cust_date=`expr $input_minue/86400`"-"`expr $input_minue%86400` | awk ‘{printf("%02dn",$0)}‘":"$input_minue":00"
echo ‘cust_date:‘ ${cust_date}
echo ‘run_time2:‘ $run_time2
if [ ${cust_date}<${run_time2} ]
then
echo "超过一天,没有超时"
else
echo "超过一天,超时"
cmd="kill -9 "$i
eval $cmd
echo $cmd
fi
else
echo "small 10"
# 没有超过一天
cust_date=${input_minue}"00"
run_time2=${run_time2//:/}
if [ ${#run_time2} -eq 6 ]
then
run_time2=${run_time2:0:4}
fi
echo "cust_date:" $cust_date
echo "run_time2:" $run_time2
if [ ${cust_date} -lt ${run_time2} ]
then
echo "超时"
cmd="kill -9 "$i
eval $cmd
echo $cmd
else
echo "没有超时"
fi
fi
fi
done
fi