hue_mem_cron.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/`basename "$0" | awk -F\. '{print $1}'`.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. if [[ ! ${USER} =~ .*root* ]]
  34. then
  35. echo "Script must be run as root: exiting"
  36. exit 1
  37. fi
  38. PS_COMMAND="ps aux | grep [r]uncherrypyserver | awk '{print \$6\" \"\$2\" \"\$3\" \"\$12}'"
  39. PS_RESULTS=`eval ${PS_COMMAND}`
  40. if [[ -z ${PS_RESULTS} ]]
  41. then
  42. PS_COMMAND="ps aux | grep [r]unspawningserver | awk '{print \$6\" \"\$2\" \"\$3\" \"\$12}'"
  43. PS_RESULTS=`eval ${PS_COMMAND}`
  44. fi
  45. if [[ -z ${PS_RESULTS} ]]
  46. then
  47. echo "No Hue processes found: runcherrypyserver or runspawningserver process must be running"
  48. exit 1
  49. fi
  50. MEM=`echo ${PS_RESULTS} | awk '{print $1}'`
  51. MEM_MB=`expr ${MEM} / 1024`
  52. PID=`echo ${PS_RESULTS} | awk '{print $2}'`
  53. CPU=`echo ${PS_RESULTS} | awk '{print $3}'`
  54. PROC=`echo ${PS_RESULTS} | awk '{print $4}'`
  55. DATE=`date '+%Y%m%d-%H%M'`
  56. if [[ -f ${LOG_FILE} ]]
  57. then
  58. LOG_SIZE=`du -sm ${LOG_FILE} | awk '{print $1}'`
  59. if [[ ${LOG_SIZE} -gt ${ROTATE_SIZE} ]]
  60. then
  61. mv ${LOG_FILE} ${LOG_FILE}.1
  62. fi
  63. fi
  64. debug "${DATE} - PID: ${PID} - CPU: ${CPU} - MEM: ${MEM} - MEM_MB: ${MEM_MB} - PROC: ${PROC}"
  65. if [ ${MEM_MB} -gt ${KILL_ME} ]
  66. then
  67. echo "${DATE} - Killing Hue Process: Too much memory: ${MEM_MB} : PID: ${PID}" >> ${LOG_FILE}
  68. kill ${PID}
  69. sleep 30
  70. PS_RESULTS=`eval ${PS_COMMAND}`
  71. PID2=`echo ${PS_RESULTS} | awk '{print $2}'`
  72. if [[ ${PID} == ${PID2} ]]
  73. then
  74. kill -9 ${PID}
  75. fi
  76. fi
  77. }
  78. debug()
  79. {
  80. if [[ ! -z $VERBOSE ]]
  81. then
  82. echo "$1" >> ${LOG_FILE}
  83. fi
  84. }
  85. main "$@"