geoserver_tomcat 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/bin/bash
  2. #
  3. # This shell script takes care of starting and stopping Geoserver deployed in Apache Tomcat
  4. # It also handles killing Geoserver in case it doesn’t stop gracefully
  5. # It uses a PID file to determine the process ID so it should work with multiple Tomcat instances on one server
  6. # Just copy the script and change $INSTANCE variable to run multiple Geoserver instances
  7. # Tested on Ubuntu 12.04
  8. #
  9. # chkconfig: - 80 20
  10. #
  11. ### BEGIN INIT INFO
  12. # Provides: Geoserver
  13. # Required-Start: $network $syslog
  14. # Required-Stop: $network $syslog
  15. # Default-Start: 2 3 4 5
  16. # Default-Stop: 0 1 6
  17. # Description: Geoserver service
  18. # Short-Description: start and stop tomcat
  19. ### END INIT INFO
  20. #
  21. # Author dragan.podvezanec@gmail.com
  22. #
  23. # Based on work by Yoryos Valotasios
  24. # Geoserver configuration section
  25. INSTANCE=1
  26. USER=geoserver
  27. GEOSERVER_DATA_DIR=/home/geoserver/data_dir
  28. GEOSERVER_HOME=/home/geoserver/geoserver$INSTANCE
  29. export GEOSERVER_DATA_DIR=$GEOSERVER_DATA_DIR
  30. export GEOSERVER_HOME=$GEOSERVER_HOME
  31. # Uncomment this if you need custom logging location
  32. #export GEOSERVER_LOG_LOCATION=$GEOSERVER_HOME/logs/geoserver.log
  33. # Java configuration section
  34. JAVA_HOME=/home/geoserver/jre
  35. JAVA_OPTS="-Xms256m -Xmx2048m -XX:+UseParallelGC -XX:+UseParallelOldGC"
  36. CATALINA_PID=/var/run/geoserver$INSTANCE.pid
  37. export JAVA_HOME=$JAVA_HOME
  38. export JAVA_OPTS=$JAVA_OPTS
  39. export CATALINA_PID=$CATALINA_PID
  40. export PATH=$JAVA_HOME/bin:$PATH
  41. # End configuration section
  42. # Number of seconds to wait after nicely requesting stop
  43. SHUTDOWN_WAIT=10
  44. geoserver_pid(){
  45. echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep $GEOSERVER_HOME | grep -v grep | awk '{ print $2 }'`
  46. }
  47. start() {
  48. pid=$(geoserver_pid)
  49. if [ -n "$pid" ]
  50. then
  51. echo "Geoserver is already running (pid: $pid)"
  52. else
  53. echo "Starting Geoserver"
  54. touch $CATALINA_PID
  55. chown $USER $CATALINA_PID
  56. /bin/su -p -s /bin/sh $USER $GEOSERVER_HOME/bin/startup.sh
  57. echo "Started Geoserver with next variables:"
  58. echo "GEOSERVER_HOME=$GEOSERVER_HOME"
  59. echo "GEOSERVER_DATA_DIR=$GEOSERVER_DATA_DIR"
  60. echo "Geoserver PID is: $(geoserver_pid)"
  61. fi
  62. return 0
  63. }
  64. stop() {
  65. pid=$(geoserver_pid)
  66. if [ -n "$pid" ]
  67. then
  68. /bin/su -p -s /bin/sh $USER $GEOSERVER_HOME/bin/shutdown.sh
  69. echo -n "Stopping Geoserver"
  70. let kwait=$SHUTDOWN_WAIT
  71. count=0;
  72. until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
  73. do
  74. echo -n ".";
  75. sleep 1
  76. let count=$count+1;
  77. done
  78. if [ $count -gt $kwait ]; then
  79. echo "process is still running after $SHUTDOWN_WAIT seconds, killing process"
  80. kill $pid
  81. sleep 3
  82. # if it’s still running use kill -9
  83. if [ `ps -p $pid | grep -c $pid` -gt '0' ]; then
  84. echo "process is still running, using kill -9"
  85. kill -9 $pid
  86. sleep 3
  87. fi
  88. fi
  89. if [ `ps -p $pid | grep -c $pid` -gt '0' ]; then
  90. echo "process is still running, I give up"
  91. else
  92. # success, delete PID file
  93. rm $CATALINA_PID
  94. fi
  95. else
  96. echo "Geoserver $INSTANCE is not running"
  97. fi
  98. return 0
  99. }
  100. case $1 in
  101. start)
  102. start
  103. ;;
  104. stop)
  105. stop
  106. ;;
  107. restart)
  108. stop
  109. start
  110. ;;
  111. status)
  112. pid=$(geoserver_pid)
  113. if [ -n "$pid" ]
  114. then
  115. echo "Geoserver $INSTANCE is running with pid: $pid"
  116. else
  117. echo "Geoserver $INSTANCE is not running"
  118. fi
  119. ;;
  120. esac
  121. exit 0