Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #!/bin/bash
  2. if [ $(id -u) -ne 0 ]; then
  3. echo "must be run as root"
  4. exit 1
  5. fi
  6.  
  7. for U in $(ps -e -o user | tail -n +2 | sort | uniq); do
  8. TOTAL_RSS=0
  9. PS_COUNT=0
  10.  
  11. while read CPID CRSS; do
  12. PS_COUNT=$(expr ${PS_COUNT} + 1)
  13. TOTAL_RSS=$(expr ${TOTAL_RSS} + ${CRSS})
  14. MAX_RSS=${CRSS}
  15. MAX_RSS_PID=${CPID}
  16. done < <(ps -u ${U} -o pid,rss | tail -n +2 | sort -n -k 2)
  17.  
  18. if [ ${PS_COUNT} -gt 0 ]; then
  19. AVGRSS=$(expr ${TOTAL_RSS} / ${PS_COUNT})
  20. echo "${U} ${PS_COUNT} ${TOTAL_RSS}"
  21.  
  22. if [ ${MAX_RSS} -gt $(expr ${AVGRSS} "*" 2) ]; then
  23. echo -e "\tpid ${MAX_RSS_PID} has ${MAX_RSS}, will terminate"
  24. kill -s SIGTERM ${MAX_RSS_PID}
  25. sleep 2
  26. kill -s SIGKILL ${MAX_RSS_PID}
  27. fi
  28. fi
  29. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement