应同事要求,写了个shell, 主要功能为查找超时的进程,并关闭!
调用方式:
sh monter_shell_minute.sh shell_sleep 30
shell_sheep : 为进程名
30 : 为30分钟
从打印的日志能看出会多两个PID,不要惊慌,由于你执行时会带有 搜索的“程序名”,且执行时也会产生一个,所以会有两个pid(运行时间比较短)。
也可以在crontab 中写任务,每隔一段时间执行一次。。
#!/bin/bash # author : jackical # lastchagetime : 2019-01-15 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=$' ' 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 1440 ] 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/1440`"-"`expr $input_minue%1440/60` | awk '{printf("%02d ",$0)}'":"`expr $input_minue%86400` | awk '{printf("%02d ",$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 6 ] then run_time2=${run_time2:0:4} cust_date=`expr $input_minue/60`""`expr $input_minue%60` else cust_date=${input_minue}"00" 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”的尴尬..