shell脚本定时kill超过1小时的指定命令

前端之家收集整理的这篇文章主要介绍了shell脚本定时kill超过1小时的指定命令前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

实践中,经常发现有些shell命令长期执行,于是整理一程序,crontab定时清理下:

  1. #!/bin/sh
  2.  
  3. p_name=phantomJs
  4. while true
  5. do
  6. sleep 3
  7. #every 3s check
  8. pid="$(ps -ef|grep $p_name|awk '{print $2}'|head -n1)"
  9. echo $pid
  10. ptime="$(ps -eo pid,etime|grep $pid|awk '{print $2}' |head -n1)"
  11. echo $ptime
  12. pstatus="$(echo $ptime|awk '{split($1,tab,/:/); if (tab[2]+tab[1]*60>=60) {print 1}else{print 0} }')"
  13. echo $pstatus
  14. #if time > 1 hour,return 0,else 1
  15. if [ $pstatus="1" ];then
  16. kill -9 $pid
  17. echo "kill success " $pid
  18. fi
  19. done

猜你在找的Bash相关文章