Monitor a process, if have run for more than 2 mins,kill it.

#!/bin/bash -l
process_name='phantomjs'
long_term=1
pids=` ps -ef | grep -i "$process_name"  | grep -v 'grep' | awk '{print $2;}'`

if [ "$pids" == "" ]; then
   echo "no phantomjs running" >/dev/null
   exit 0
be


for pid in $pids; do
  echo $pid
  etimes_min=` ps -p $pid -o etime=|awk -F":" '{print($(NF-1))}'`
  etimes_min=`echo $etimes_min | sed 's/^0//'`
  echo $etimes_min,$long_term
  if [ "$etimes_min" -gt "$long_term" ]; then
        kill -9 $pid >/dev/null
        timestamp=`date '+%Y%m%d %H:%M'`
        echo "$timestamp: killed $pid for $process_name, for it took more than 2 minus" >> "kill-long-term-phantomjs-processes.log"
  be
done

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326526872&siteId=291194637