hue_mem_cron.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/bash
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #Cron script designed to kill Hue instances that are utilizing too much memory.
  18. #If using CM, then configure CM to restart the Hue process when it dies and
  19. #then Hue will restart after this script kills it. Otherwise, modify this script
  20. #to start Hue again.
  21. KILL_ME=5000 #This is the number of MB at which it will kill.
  22. #Starting with 5000(5gb)
  23. VERBOSE=true #true then this writes out the proc info each time it runs, leave blank
  24. #to only write out when we kill the process
  25. LOG_FILE=/var/log/hue_mem_cron.log
  26. ROTATE_SIZE=10 #MB before rotating, size in MB before rotating log to .1, we only keep
  27. #2 log files, so 20MB max
  28. main()
  29. {
  30. # Command to get memory usage. This will handle multiple Hue instances per server, so
  31. # this command grabs the highest memory Hue process at the time of running and
  32. # kills it. Then the next run it'll get the next process.
  33. PS_COMMAND=`ps aux | grep [r]uncherrypyserver | awk '{print $6" "$2" "$3" "$12}'`
  34. MEM=`echo ${PS_COMMAND} | awk '{print $1}'`
  35. MEM_MB=`expr ${MEM} / 1024`
  36. PID=`echo ${PS_COMMAND} | awk '{print $2}'`
  37. CPU=`echo ${PS_COMMAND} | awk '{print $3}'`
  38. PROC=`echo ${PS_COMMAND} | awk '{print $4}'`
  39. DATE=`date '+%Y%m%d-%H%M'`
  40. if [[ -f ${LOG_FILE} ]]
  41. then
  42. LOG_SIZE=`du -sm ${LOG_FILE} | awk '{print $1}'`
  43. if [ ${LOG_SIZE} -gt ${ROTATE_SIZE} ]
  44. then
  45. mv ${LOG_FILE} ${LOG_FILE}.1
  46. fi
  47. fi
  48. debug "${DATE} - PID: ${PID} - CPU: ${CPU} - MEM: ${MEM} - MEM_MB: ${MEM_MB} - PROC: ${PROC}"
  49. if [ ${MEM_MB} -gt ${KILL_ME} ]
  50. then
  51. echo "${DATE} - Killing Hue Process: Too much memory: ${MEM_MB} : PID: ${PID}" >> ${LOG_FILE}
  52. kill ${PID}
  53. sleep 30
  54. PID2=`ps aux | grep [r]uncherrypyserver | awk '{print $2}'`
  55. if [[ ${PID} == ${PID2} ]]
  56. then
  57. kill -9 ${PID}
  58. fi
  59. fi
  60. }
  61. debug()
  62. {
  63. if [[ ! -z $VERBOSE ]]
  64. then
  65. echo "$1" >> ${LOG_FILE}
  66. fi
  67. }
  68. main "$@"