Linux: Kill Process based on Start Time (STIME)

One of the server that I am working with has some infinitely running PHP process. Due to incorrect way of cron setup by the development team, it has caused the process hanging and not ended properly. According to them, these processes can be killed if still hang after 12 hours.

Any process which run in server will have start time (STIME). You can check this by using ps command. In this case, following result will appear:

$ ps aux | grep php
root      1399  0.0  0.0  61188   740 pts/2    S+   10:10   0:00 grep php
user1  2697  0.0  0.0 100664  8340 ?        Ss   Jul04   0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1  5551  0.0  0.4 171052 78832 ?        Ss   Jun25   0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1  9913  0.0  0.5 174636 82392 ?        Ss   Jun22   0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 11961  0.0  0.7 223276 131060 ?       Ss   May25   0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 16455  0.0  0.4 171564 79420 ?        Ss   Jun24   0:01 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 17474  0.0  0.5 182060 90016 ?        Ss   Jun18   0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 20094  0.0  0.6 206636 114588 ?       Ss   Jun03   0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 22555  0.0  0.7 213548 121476 ?       Ss   May30   0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 24670  0.0  0.7 214572 122320 ?       Ss   May30   0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 28200  0.0  0.7 220204 127988 ?       Ss   May26   0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 30832  0.0  0.4 170284 78168 ?        Ss   Jun25   0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 30837  0.0  0.4 170114 88508 ?        Ss   23:20   0:00 /usr/local/bin/php /home/user1/cron/sync2server.php
user1 30848  0.0  0.4 120439 80770 ?        Ss   12:20   0:00 /usr/local/bin/php /home/user1/cron/sync2server.php

If you see the STIME value mostly has started long time ago but it is still inside the process list. To kill all the older process which more that 24 hours, I use following command:

$ kill -9 `ps aux | grep php | grep sync2server | awk '$9 !~ /[0-9]:[0-9]/' | awk '{print $2}'`

Explanation:
Using ps command, we grep anything related to “php” and “sync2server” which is the specific process that we want to kill. The 4th argument is checking whether column no 9 (STIME) column has value which NOT in “number:number” format. Process which starts for more than 24 hours, STIME value will contains word for example “Jun23” or “2010”. The 5th argument is actually print the value of column no 2 which is the PID to be killed.

To kill process which less than 24 hours, you can use following script:

LIMIT=43200 #limit on seconds = 12 hours
PROCESS="php" #change to process u want to grep
 
count=`ps aux | grep $PROCESS | awk {'print $9'} | wc -l`
 
for ((i=1;i<=$count;i++))
do
    ptime=`ps aux | grep $PROCESS | awk {'print $9'} | head -$i | tail -1`
    psec=`date "+%s" -d "$ptime"`
    csec=`date "+%s"`
    exectime=$((csec-psec))
 
    if [ $exectime -gt $LIMIT ]
    then
        pid=`ps aux | grep $PROCESS | awk {'print $2'} | head -$i | tail -1`
        /bin/kill -9 $pid
    fi
done

Above scripts will try to find any PHP process which executed more than 12 hours and will kill it one by one. You can get the script to run as cron twice per day so it will automate your administration job.

Lets share if you have better idea. Cheers!

2 thoughts on “Linux: Kill Process based on Start Time (STIME)

  1. it’s what I’m looking for, but I think it’s a little off, that start time is what hr:min of the day the process started, not how many hrs:mins it has been running. So your script is killing the processes that start after 10am, not ones that have been running for more than 10 hrs.

    Reply

    1. I am aware of STIME is the time when the process started. Thats why when you refer to line:11, I have exectime=$((csec-psec)) where csec is the current time in seconds and psec is the start time in seconds. It will then compare the result with $LIMIT we set.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *