php脚本执行进程30分钟内不退出的话,就kill掉这些php的脚本进程
admin
2023-02-25 14:00:08
0

线上脚本内容如下:
[root@localhost ~]# cat /data/scripts/check_php.sh

#!/bin/bash
Date=`date "+%Y-%m-%d %H:%M:%S"`
Num=$(ps -ef|egrep  "countjs_syc_site*|countjs_syc_plan*|countjs_syc.php|countjs_syc_img*|setcache*"|grep -v grep |wc -l)
Pid=$(/bin/ps -ef|egrep  "countjs_syc_site*|countjs_syc_plan*|countjs_syc.php|countjs_syc_img*|setcache*"|grep -v grep| awk '{print $2}')

if [ $Num -eq 0 ];then
  echo "$Date No Process" >> /data/scripts/check_php.log
else
  for i in $Pid
    do
        echo "$Date Running Process $i"   >> /data/scripts/check_php.log 
##define Get Seconds var
Sec=$(ps -p $i  h -o etime|tail -1|awk 'BEGIN { FS = ":" }{if (NF == 2) {print $1*60 + $2 } else if (NF == 3) {split($1, a, "-");if (a[2] != "" ) {print ((a[1]*24+a[2])*60 + $2) * 60 + $3; } else {print ($1*60 + $2) * 60 + $3;}}}')

        if [ $Sec -ge 1800 ];then
            echo $Date >> /data/scripts/check_php.log
            ps -ef | grep $i | grep -v grep  >> /data/scripts/check_php.log
            kill -9 $i
            echo "$Date $i The process runs for more than 30 minutes,The process has been killed." >> /data/scripts/check_php.log
        else
            echo "$Date $i The process runs in less than 30 minutes" >> /data/scripts/check_php.log
        fi

    done
fi
echo '===========================' >> /data/scripts/check_php.log

另外一种脚本形式如下:
cat /root/scripts.awk

#!/usr/bin/awk -f  
BEGIN { FS = ":" }
{
  if (NF == 2) {
    print $1*60 + $2
  } else if (NF == 3) {
    split($1, a, "-");
    if (a[2] != "" ) {
      print ((a[1]*24+a[2])*60 + $2) * 60 + $3;
    } else {
      print ($1*60 + $2) * 60 + $3;
    }
  }
}

脚本分析如下:

ps -ef|grep    18020 |grep -v grep|awk '{print $2}'; ps -p  18020  h -o etime|tail -1|awk 'BEGIN { FS = ":" }{if (NF == 3) {split($1, a, "-");if (a[2] != "" ) {print ((a[1]*24+a[2])*60 + $2) * 60 + $3; } else {print ($1*60 + $2) * 60 + $3;}}}'

echo "   03:19:15"|awk 'BEGIN { FS = ":" }{if (NF == 3) {split($0, a, ":");if (a[1] != "" ){print a[1],a[2] }}}'

echo "   03:19:15"|awk 'BEGIN { FS = ":" }{if (NF == 3) {split($0, a, ":");if (a[1] != "" ){print a[1],a[3] }}}'

ps -p 18020  -o etime|tail -1|awk 'BEGIN { FS = ":" }{if (NF == 3) {split($0, a, ":");if (a[2] != "" ) {print a[1],a[2],a[3] };{print ($1*60 + $2) * 60 + $3;}}}'
   03 54 06
14046

awk 字符串处理函数,split

split(s,a,fs)    以fs为分割符 将s字符串分成序列a
split可以实现对字符串进行数组类型的分割,下面用例子来说明下。
[root@localhost ~]# echo 'abcd'?| awk '{len=split($0,a,"");for(i=1;i<=len;i++)print "a["i"]="a[i];print "length="len}'
a[1]=a
a[2]=b
a[3]=c
a[4]=d
a[5]=?
length=5

解析说明:首先把abcd换为一个数组,并且数组的分隔符为没有符号,len=split($0,a,"")为获取了整个数组的长度,之后进行输出。在awk中如果是当做字符串输出的字符,全部用双引号来引起来。

查看php脚本运行的时间:

[root@localhost ~]# ps -p 5493  h -o etime
 1-01:35:00
运行了1天1小时35分00秒
根据进程号过滤出具体的运行脚本名称:
[root@localhost ~]# ps -ef|grep 5493|grep -v grep
root      5493  5490  0 9月18 ?       00:00:00 /bin/bash /data/cron/chksh/setcache.sh
root      5506  5493  0 9月18 ?       00:00:00 /usr/bin/php /data/cron/ptask/setcache.php

[root@localhost ~]#  ps -p 5493  h -o etime|tail -1
 1-01:40:58

[root@localhost ~]#  ps -p 5493  h -o etime|tail -1|awk 'BEGIN { FS = ":" }{if (NF == 3) {split($1, a, "-");if (a[2] != "" ) {print a[1],a[2],a[3]}}}'
 1 01

计算进程运行的分钟数:

[root@localhost ~]# ps -p 5493  h -o etime|tail -1
 1-01:47:56

[root@localhost ~]# ps -p 5493  h -o etime|tail -1|awk 'BEGIN { FS = ":" }{if (NF == 3) {split($1, a, "-");if (a[2] != "" ) {print ((a[1]*24+a[2])*60 + $2)}}}'
1547
计算进程运行的秒数:
[root@localhost ~]# ps -p 5493  h -o etime|tail -1|awk 'BEGIN { FS = ":" }{if (NF == 3) {split($1, a, "-");if (a[2] != "" ) {print ((a[1]*24+a[2])*60 + $2)*60 + $3}}}'
93036

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

[root@localhost ~]# ps -p33920 h -o etime
   04:31:49

[root@localhost ~]# ps -p33920 h -o etime|awk 'BEGIN { FS = ":" }{if (NF == 3) {split($1, a, "-");if (a[2] != "" ) {print a[1]}}}'
[root@localhost ~]# ps -p33920 h -o etime|awk 'BEGIN { FS = ":" }{if (NF == 3) {split($1, a, "-");if (a[2] != "" ) {print a[2]}}}'
[root@localhost ~]# ps -p33920 h -o etime|awk 'BEGIN { FS = ":" }{if (NF == 3) {split($1, a, "-");if (a[2] != "" ) {print $2 }}}'
[root@localhost ~]# ps -p33920 h -o etime|awk 'BEGIN { FS = ":" }{if (NF == 3) {split($1, a, "-");if (a[2] != "" ) {print $3 }}}'

此时数值都为空

所以此时采用下面的公式计算秒数也为空:

 ps -p33920 h -o etime|awk 'BEGIN { FS = ":" }{if (NF == 3) {split($1, a, "-");if (a[2] != "" ) {print ((a[1]*24+a[2])*60 + $2)*60 + $3}}}'

然而采用下面的公式来计算是正确的:

[root@localhost ~]# ps -p33920 h -o etime|awk 'BEGIN { FS = ":" }{if (NF == 3) {split($1, a, "-");if (a[2] != "" ) {print ((a[1]*24+a[2])*60 + $2)*60 + $3} else {print ($1*60 + $2) * 60 + $3;}}}'
16908
[root@localhost ~]# ps -p33920 h -o etime && ps -p33920 h -o etime|awk 'BEGIN { FS = ":" }{if (NF == 3) {split($1, a, "-");if (a[2] != "" ) {print ((a[1]*24+a[2])*60 + $2)*60 + $3} else {print ($1*60 + $2) * 60 + $3;}}}'
   04:44:18  实际时间格式
17058     一共秒数
ps -p 33573  h -o etime|tail -1|awk 'BEGIN { FS = ":" }{if (NF == 2) {print $1*60 + $2 } else if (NF == 3) {split($1, a, "-");if (a[2] != "" ) {print ((a[1]*24+a[2])*60 + $2) * 60 + $3; } else {print ($1*60 + $2) * 60 + $3;}}}

相关内容

热门资讯

【第一资讯】“阿道夫十三水.辅... 有 亲,根据资深记者爆料阿道夫十三水是可以开挂的,确实有挂(咨询软件无需...
玩家攻略科普“情怀古诗词.开挂... 有 亲,根据资深记者爆料情怀古诗词是可以开挂的,确实有挂(咨询软件无需打...
玩家最新攻略“樱花炸金花.开挂... 网上科普关于“樱花炸金花有没有挂”话题很是火热,小编也是针对樱花炸金花作*弊开挂的方法以及开挂对应的...
盐城固态电池产线落地经开区,未... 12月21日,固态离子能源科技(武汉)有限公司在盐城举办固态动力电池技术成果发布活动,中外行业专家、...
玩家分享攻略“YY棋牌.辅助开... 网上科普关于“YY棋牌有没有挂”话题很是火热,小编也是针对YY棋牌作*弊开挂的方法以及开挂对应的知识...
重磅消息“衡阳十胡卡.开挂器?... 重磅消息“衡阳十胡卡.开挂器?”确实真的有挂您好,衡阳十胡卡这个游戏其实有挂的,确实是有挂的,需要了...
【第一消息】“微乐海南麻将.有... 【第一消息】“微乐海南麻将.有挂吗?”太坑了原来有挂您好,微乐海南麻将这个游戏其实有挂的,确实是有挂...
【第一消息】“飞鹰互娱.可以开... 您好:飞鹰互娱这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款游戏...
【今日要闻】“九九山城麻将.开... 家人们!今天小编来为大家解答九九山城麻将透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里...
今日重大发现“新皇豪炸金花.到... 今日重大发现“新皇豪炸金花.到底有挂吗?”确实真的有挂您好,新皇豪炸金花这个游戏其实有挂的,确实是有...