hue_mem_cron.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/bash
  2. #Cron script designed to kill Hue instances that are utilizing too much memory.
  3. #If using CM, then configure CM to restart the Hue process when it dies and
  4. #then Hue will restart after this script kills it. Otherwise, modify this script
  5. #to start Hue again.
  6. KILL_ME=5000 #This is the number of MB at which it will kill.
  7. #Starting with 5000(5gb)
  8. VERBOSE=true #true then this writes out the proc info each time it runs, leave blank
  9. #to only write out when we kill the process
  10. LOG_FILE=/var/log/hue_mem_cron.log
  11. ROTATE_SIZE=10 #MB before rotating, size in MB before rotating log to .1, we only keep
  12. #2 log files, so 20MB max
  13. main()
  14. {
  15. # Command to get memory usage. This will handle multiple Hue instances per server, so
  16. # this command grabs the highest memory Hue process at the time of running and
  17. # kills it. Then the next run it'll get the next process.
  18. PS_COMMAND=`ps aux | grep [r]uncherrypyserver | awk '{print $6" "$2" "$3" "$12}'`
  19. MEM=`echo ${PS_COMMAND} | awk '{print $1}'`
  20. MEM_MB=`expr ${MEM} / 1024`
  21. PID=`echo ${PS_COMMAND} | awk '{print $2}'`
  22. CPU=`echo ${PS_COMMAND} | awk '{print $3}'`
  23. PROC=`echo ${PS_COMMAND} | awk '{print $4}'`
  24. DATE=`date '+%Y%m%d-%H%M'`
  25. if [[ -f ${LOG_FILE} ]]
  26. then
  27. LOG_SIZE=`du -sm ${LOG_FILE} | awk '{print $1}'`
  28. if [ ${LOG_SIZE} -gt ${ROTATE_SIZE} ]
  29. then
  30. mv ${LOG_FILE} ${LOG_FILE}.1
  31. fi
  32. fi
  33. debug "${DATE} - PID: ${PID} - CPU: ${CPU} - MEM: ${MEM} - MEM_MB: ${MEM_MB} - PROC: ${PROC}"
  34. if [ ${MEM_MB} -gt ${KILL_ME} ]
  35. then
  36. echo "${DATE} - Killing Hue Process: Too much memory: ${MEM_MB} : PID: ${PID}" >> ${LOG_FILE}
  37. kill ${PID}
  38. sleep 30
  39. PID2=`ps aux | grep [r]uncherrypyserver | awk '{print $2}'`
  40. if [[ ${PID} == ${PID2} ]]
  41. then
  42. kill -9 ${PID}
  43. fi
  44. fi
  45. }
  46. debug()
  47. {
  48. if [[ ! -z $VERBOSE ]]
  49. then
  50. echo "$1" >> ${LOG_FILE}
  51. fi
  52. }
  53. main "$@"