Advertisement
Guest User

StartupScript

a guest
Aug 5th, 2016
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # description: Apache Tomcat init script
  4. # processname: tomcat
  5. # chkconfig: 234 20 80
  6. #
  7. #
  8. # Copyright (C) 2014 Miglen Evlogiev
  9. #
  10. # This program is free software: you can redistribute it and/or modify it under
  11. # the terms of the GNU General Public License as published by the Free Software
  12. # Foundation, either version 3 of the License, or (at your option) any later
  13. # version.
  14. #
  15. # This program is distributed in the hope that it will be useful, but WITHOUT
  16. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along with
  20. # this program. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. # Initially forked from: gist.github.com/valotas/1000094
  23. # Source: gist.github.com/miglen/5590986
  24.  
  25.  
  26. #Location of JAVA_HOME (bin files)
  27. export JAVA_HOME=/usr/java/jdk1.8.0_92
  28.  
  29. #Add Java binary files to PATH
  30. export PATH=$JAVA_HOME/bin:$PATH
  31.  
  32. #CATALINA_HOME is the location of the bin files of Tomcat
  33. export CATALINA_HOME=/home/wasodost/apache-tomcat-8.0.35
  34.  
  35. #CATALINA_BASE is the location of the configuration files of this instance of Tomcat
  36. export CATALINA_BASE=/home/wasodost/apache-tomcat-8.0.35
  37.  
  38. #TOMCAT_USER is the default user of tomcat
  39. export TOMCAT_USER=root
  40.  
  41. #TOMCAT_USAGE is the message if this script is called without any options
  42. TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;31mkill\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"
  43.  
  44. #SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
  45. SHUTDOWN_WAIT=20
  46.  
  47. tomcat_pid() {
  48. echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "|cut -d" " -f2`
  49. }
  50.  
  51. start() {
  52. pid=$(tomcat_pid)
  53. if [ -n "$pid" ]
  54. then
  55. echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
  56. else
  57. # Start tomcat
  58. echo -e "\e[00;32mStarting tomcat\e[00m"
  59. #ulimit -n 100000
  60. #umask 007
  61. #/bin/su -p -s /bin/sh $TOMCAT_USER
  62. if [ `user_exists $TOMCAT_USER` = "1" ]
  63. then
  64. /bin/su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
  65. else
  66. echo -e "\e[00;31mTomcat user $TOMCAT_USER does not exists. Starting with $(id)\e[00m"
  67. sh $CATALINA_HOME/bin/startup.sh
  68. fi
  69. status
  70. fi
  71. return 0
  72. }
  73.  
  74. status(){
  75. pid=$(tomcat_pid)
  76. if [ -n "$pid" ]
  77. then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
  78. else
  79. echo -e "\e[00;31mTomcat is not running\e[00m"
  80. return 3
  81. fi
  82. }
  83.  
  84. terminate() {
  85. echo -e "\e[00;31mTerminating Tomcat\e[00m"
  86. kill -9 $(tomcat_pid)
  87. }
  88.  
  89. stop() {
  90. pid=$(tomcat_pid)
  91. if [ -n "$pid" ]
  92. then
  93. echo -e "\e[00;31mStoping Tomcat\e[00m"
  94. #/bin/su -p -s /bin/sh $TOMCAT_USER
  95. sh $CATALINA_HOME/bin/shutdown.sh
  96.  
  97. let kwait=$SHUTDOWN_WAIT
  98. count=0;
  99. until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
  100. do
  101. echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
  102. sleep 1
  103. let count=$count+1;
  104. done
  105.  
  106. if [ $count -gt $kwait ]; then
  107. echo -n -e "\n\e[00;31mkilling processes didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
  108. terminate
  109. fi
  110. else
  111. echo -e "\e[00;31mTomcat is not running\e[00m"
  112. fi
  113.  
  114. return 0
  115. }
  116.  
  117. user_exists(){
  118. if id -u $1 >/dev/null 2>&1; then
  119. echo "1"
  120. else
  121. echo "0"
  122. fi
  123. }
  124.  
  125. case $1 in
  126. start)
  127. start
  128. ;;
  129. stop)
  130. stop
  131. ;;
  132. restart)
  133. stop
  134. start
  135. ;;
  136. status)
  137. status
  138. exit $?
  139. ;;
  140. kill)
  141. terminate
  142. ;;
  143. *)
  144. echo -e $TOMCAT_USAGE
  145. ;;
  146. esac
  147. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement