加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

[shell]关闭超时的进程

发布时间:2020-12-15 23:04:40 所属栏目:安全 来源:网络整理
导读:应同事要求,写了个shell,主要功能为查找超时的进程,并关闭! 调用方式: sh monter_shell_minute. sh shell_sleep 30 shell_sheep :? 为进程名 30 : 为30分钟 从打印的日志能看出会多两个PID,不要惊慌,由于你执行时会带有 搜索的“程序名”,且执行时

应同事要求,写了个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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读