应同事要求,写了个shell, 主要功能为查找超时的进程,并关闭!
调用方式:
sh monter_shell_minute.sh shell_sleep
shell_sheep : 为进程名
30 : 为30分钟
从打印的日志能看出会多两个PID,不要惊慌,由于你执行时会带有 搜索的“程序名”,且执行时也会产生一个,所以会有两个pid(运行时间比较短)。
也可以在crontab 中写任务,每隔一段时间执行一次。。
#!/bin/bash # author : jackical
# lastchagetime : -- input_program=$
input_minue=$ 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 -a $input_minue -lt ]
then
echo "已超时!"
cmd="kill -9 "$i
eval $cmd
echo $cmd
else
if [ ${#run_time2} -gt ]
then
echo "big 10"
cust_date=`expr $input_minue/`"-"`expr $input_minue%/` | awk '{printf("%02d\n",$0)}'":"`expr $input_minue%` | awk '{printf("%02d\n",$0)}'":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"
# 没有超过一天
run_time2=${run_time2//:/}
if [ ${#run_time2} -eq ]
then
run_time2=${run_time2::}
cust_date=`expr $input_minue/`""`expr $input_minue%`
else
cust_date=${input_minue}""
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
最后建议在linux 下新建个文件,再把内容贴进去。从本地windows环境下上传shell到linux 环境,会遇到“一脸懵B”的尴尬..