欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Monitor a process, if have run for more than 2 mins,kill it. 博客分类: Shell  

程序员文章站 2024-03-13 08:59:57
...
#!/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
fi


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"
  fi
done