If the php script execution process does not exit within 30 minutes, kill these php script processes

Keywords: Linux PHP less

The online script content is as follows:
[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

Another form of script is as follows:
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;
    }
  }
}

The script is analyzed as follows:

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 string handler, split

split(s,a,fs)    with fs Set the s String splitting sequence a
split You can divide the string into array types. Here is an example.
[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

Explanation: first, change the abcd to an array, and the separator of the array is unsigned. len=split( Len = split ($0, a,,a, "") gets the length of the entire array, and then output. In awk, if the characters are output as strings, they are all enclosed in double quotes.

To see when the php script runs:

[root@localhost ~]# ps -p 5493  h -o etime
 1-01:35:00
//One day, one hour, 35 minutes and 00 seconds
//Filter out the specific running script name according to the process number:
[root@localhost ~]# ps -ef|grep 5493|grep -v grep
root      5493  5490  0 9 Month 18 ?       00:00:00 /bin/bash /data/cron/chksh/setcache.sh
root      5506  5493  0 9 Month 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

Count the minutes the process is running:

[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
//Count the number of seconds the process is running:
[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 }}}'

//At this time, the values are all empty

Therefore, the following formula is used to calculate the number of seconds

 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}}}'

However, the following formula is correct:

[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  Actual time format
17058     Total seconds
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;}}}

Posted by billf2007 on Sat, 07 Dec 2019 18:48:28 -0800