1
0

Tomcat-init 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: tomcat
  4. # Required-Start: $remote_fs $syslog
  5. # Required-Stop: $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: tomcat-server daemon
  9. # Description: tomcat-server daemon
  10. ### END INIT INFO
  11. #
  12. # chkconfig: - 95 15
  13. # description: Tomcat start/stop/status script
  14. #Location of JAVA_HOME (bin files)
  15. export JAVA_HOME=
  16. #Add Java binary files to PATH
  17. export PATH=$JAVA_HOME/bin:$PATH
  18. #CATALINA_HOME is the location of the configuration files of this instance of Tomcat
  19. CATALINA_HOME=/usr/local/tomcat
  20. #TOMCAT_USER is the default user of tomcat
  21. TOMCAT_USER=www
  22. #TOMCAT_USAGE is the message if this script is called without any options
  23. TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"
  24. #SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
  25. SHUTDOWN_WAIT=20
  26. tomcat_pid() {
  27. echo `ps -ef | grep java | grep $CATALINA_HOME/ | grep -v grep | tr -s " "|cut -d" " -f2`
  28. }
  29. start() {
  30. pid=$(tomcat_pid)
  31. if [ -n "$pid" ]; then
  32. echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
  33. else
  34. echo -e "\e[00;32mStarting tomcat\e[00m"
  35. if [ `user_exists $TOMCAT_USER` = "1" ]; then
  36. su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
  37. else
  38. $CATALINA_HOME/bin/startup.sh
  39. fi
  40. status
  41. fi
  42. return 0
  43. }
  44. status() {
  45. pid=$(tomcat_pid)
  46. if [ -n "$pid" ]; then
  47. echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
  48. else
  49. echo -e "\e[00;31mTomcat is not running\e[00m"
  50. fi
  51. }
  52. stop() {
  53. pid=$(tomcat_pid)
  54. if [ -n "$pid" ]; then
  55. echo -e "\e[00;31mStoping Tomcat\e[00m"
  56. $CATALINA_HOME/bin/shutdown.sh
  57. let kwait=$SHUTDOWN_WAIT
  58. count=0;
  59. until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
  60. do
  61. echo -n -e "\e[00;31mwaiting for processes to exit\e[00m\n";
  62. sleep 1
  63. let count=$count+1;
  64. done
  65. if [ $count -gt $kwait ]; then
  66. echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
  67. kill -9 $pid
  68. fi
  69. else
  70. echo -e "\e[00;31mTomcat is not running\e[00m"
  71. fi
  72. return 0
  73. }
  74. user_exists() {
  75. if id -u $1 >/dev/null 2>&1; then
  76. islogin=`cat /etc/passwd | grep ^${TOMCAT_USER}: | grep nologin$`
  77. if [ "${islogin: -7}" = "nologin" ]; then
  78. echo "0"
  79. else
  80. echo "1"
  81. fi
  82. else
  83. echo "0"
  84. fi
  85. }
  86. case $1 in
  87. start)
  88. start
  89. ;;
  90. stop)
  91. stop
  92. ;;
  93. restart)
  94. stop
  95. start
  96. ;;
  97. status)
  98. status
  99. ;;
  100. *)
  101. echo -e $TOMCAT_USAGE
  102. ;;
  103. esac
  104. exit 0