pveselov

OOM restart script

Dec 15th, 2017
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.48 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. (
  4.  
  5. # we need to close all existing file descriptors, because
  6. # JVM passes them all on fork, and we end up holding up
  7. # listening sockets, causing re-use failure on tomcat startup
  8. # https://unix.stackexchange.com/questions/123413/close-all-file-descriptors-in-bash
  9. for fd in $(ls /proc/$$/fd); do
  10.     case "$fd" in
  11.         0|1|2)
  12.         ;;
  13.     *)
  14.         eval "exec $fd>&-"
  15.         ;;
  16.     esac
  17. done
  18.  
  19. if test "$1" != setsid; then
  20.     date; echo "Invoked with pid $1, restarting ourselves"
  21.     setsid $0 setsid $1 &
  22.     exit 0
  23. fi
  24.  
  25. `dirname $0`/shutdown.sh
  26. date; echo "Shutdown called"
  27. java="$2"
  28. date; echo "Waiting for $java to die"
  29. laps=200
  30. while test -e /proc/$java; do
  31.     sleep 0.1
  32.     laps=$((laps-1))
  33.     # if shutdown failed to stop the server,
  34.     # we must kill it, but this may leave
  35.     # open ports, so we delay ourselve for
  36.     # some time.
  37.     test $laps -eq 0 && {
  38.         date; echo "Waited too long, killing"
  39.         kill -9 $java
  40.         sleep 120
  41.         break;
  42.     }
  43. done
  44.  
  45. date; echo "About to restart the server"
  46.  
  47. # You can send an email, or some other notification here
  48. # Mail -s "Tomcat restarted due to OOM" << _EOF_
  49. # Tomcat PID $java has been restarted due to OOM
  50. # _EOF_
  51.  
  52. # sleeping for a while for process to die for sure
  53. sleep 5
  54.  
  55. # this script is expected to be at bin directory
  56. # of the corresponding tomcat. If not, change the path
  57.  
  58. date; echo "Calling startup.."
  59.  
  60. `dirname $0`/startup.sh
  61. ) >> /tmp/restart.log 2>&1
Advertisement
Add Comment
Please, Sign In to add comment