Advertisement
Guest User

Untitled

a guest
Apr 26th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.00 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # /etc/init.d/wildfly -- startup script for WildFly
  4. #
  5. # Written by Jorge Solorzano
  6. #
  7. ### BEGIN INIT INFO
  8. # Provides:             wildfly
  9. # Required-Start:       $remote_fs $network
  10. # Required-Stop:        $remote_fs $network
  11. # Should-Start:         $named
  12. # Should-Stop:          $named
  13. # Default-Start:        2 3 4 5
  14. # Default-Stop:         0 1 6
  15. # Short-Description:    WildFly Application Server
  16. # Description:          Provide WildFly startup/shutdown script
  17. ### END INIT INFO
  18.  
  19. NAME=wildfly
  20. DESC="WildFly Application Server"
  21. DEFAULT="/etc/default/$NAME"
  22.  
  23. # Check privileges
  24. if [ `id -u` -ne 0 ]; then
  25.     echo "You need root privileges to run this script"
  26.     exit 1
  27. fi
  28.  
  29. # Make sure wildfly is started with system locale
  30. if [ -r /etc/default/locale ]; then
  31.     . /etc/default/locale
  32.     export LANG
  33. fi
  34.  
  35. . /lib/lsb/init-functions
  36.  
  37. if [ -r /etc/default/rcS ]; then
  38.     . /etc/default/rcS
  39. fi
  40.  
  41. # Overwrite settings from default file
  42. if [ -f "$DEFAULT" ]; then
  43.     . "$DEFAULT"
  44. fi
  45.  
  46. # Location of JDK
  47. if [ -n "$JAVA_HOME" ]; then
  48.     export JAVA_HOME
  49. fi
  50.  
  51. # Setup the JVM
  52. if [ -z "$JAVA" ]; then
  53.     if [ -n "$JAVA_HOME" ]; then
  54.         JAVA="$JAVA_HOME/bin/java"
  55.     else
  56.         JAVA="java"
  57.     fi
  58. fi
  59.  
  60. # Location of wildfly
  61. if [ -z "$JBOSS_HOME" ]; then
  62.     JBOSS_HOME="/opt/wildfly"
  63. fi
  64. export JBOSS_HOME
  65.  
  66. # Check if wildfly is installed
  67. if [ ! -f "$JBOSS_HOME/jboss-modules.jar" ]; then
  68.     log_failure_msg "$NAME is not installed in \"$JBOSS_HOME\""
  69.     exit 1
  70. fi
  71.  
  72. # Run as wildfly user
  73. # Example of user creation for Debian based:
  74. # adduser --system --group --no-create-home --home $JBOSS_HOME --disabled-login wildfly
  75. if [ -z "$JBOSS_USER" ]; then
  76.     JBOSS_USER=wildfly
  77. fi
  78.  
  79. # Check wildfly user
  80. id $JBOSS_USER > /dev/null 2>&1
  81. if [ $? -ne 0 -o -z "$JBOSS_USER" ]; then
  82.     log_failure_msg "User \"$JBOSS_USER\" does not exist..."
  83.     exit 1
  84. fi
  85.  
  86. # Check owner of JBOSS_HOME
  87. #if [ ! $(stat -L -c "%U" "$JBOSS_HOME") = $JBOSS_USER ]; then
  88. #   log_failure_msg "The user \"$JBOSS_USER\" is not owner of \"$JBOSS_HOME\""
  89. #   exit 1
  90. #fi
  91.  
  92. # Startup mode of wildfly
  93. if [ -z "$JBOSS_MODE" ]; then
  94.     JBOSS_MODE=standalone
  95. fi
  96.  
  97. # Startup mode script
  98. if [ "$JBOSS_MODE" = "standalone" ]; then
  99.     JBOSS_SCRIPT="$JBOSS_HOME/bin/standalone.sh"
  100.     if [ -z "$JBOSS_CONFIG" ]; then
  101.         JBOSS_CONFIG=standalone.xml
  102.     fi
  103. else
  104.     JBOSS_SCRIPT="$JBOSS_HOME/bin/domain.sh"
  105.     if [ -z "$JBOSS_DOMAIN_CONFIG" ]; then
  106.         JBOSS_DOMAIN_CONFIG=domain.xml
  107.     fi
  108.     if [ -z "$JBOSS_HOST_CONFIG" ]; then
  109.         JBOSS_HOST_CONFIG=host.xml
  110.     fi
  111. fi
  112.  
  113. # Check startup file
  114. if [ ! -x "$JBOSS_SCRIPT" ]; then
  115.     log_failure_msg "$JBOSS_SCRIPT is not an executable!"
  116.     exit 1
  117. fi
  118.  
  119. # Check cli file
  120. JBOSS_CLI="$JBOSS_HOME/bin/jboss-cli.sh"
  121. if [ ! -x "$JBOSS_CLI" ]; then
  122.     log_failure_msg "$JBOSS_CLI is not an executable!"
  123.     exit 1
  124. fi
  125.  
  126. # The amount of time to wait for startup
  127. if [ -z "$STARTUP_WAIT" ]; then
  128.     STARTUP_WAIT=30
  129. fi
  130.  
  131. # The amount of time to wait for shutdown
  132. if [ -z "$SHUTDOWN_WAIT" ]; then
  133.     SHUTDOWN_WAIT=30
  134. fi
  135.  
  136. # Location to keep the console log
  137. if [ -z "$JBOSS_CONSOLE_LOG" ]; then
  138.     JBOSS_CONSOLE_LOG="/var/log/$NAME/console.log"
  139. fi
  140. export JBOSS_CONSOLE_LOG
  141.  
  142. # Location to set the pid file
  143. JBOSS_PIDFILE="/var/run/$NAME/$NAME.pid"
  144. export JBOSS_PIDFILE
  145.  
  146. # Launch wildfly in background
  147. LAUNCH_JBOSS_IN_BACKGROUND=1
  148. export LAUNCH_JBOSS_IN_BACKGROUND
  149.  
  150. # Helper function to check status of wildfly service
  151. check_status() {
  152.     pidofproc -p "$JBOSS_PIDFILE" "$JAVA" >/dev/null 2>&1
  153. }
  154.  
  155. case "$1" in
  156.  start)
  157.     log_daemon_msg "Starting $DESC" "$NAME"
  158.     check_status
  159.     status_start=$?
  160.     if [ $status_start -eq 3 ]; then
  161.         mkdir -p $(dirname "$JBOSS_PIDFILE")
  162.         mkdir -p $(dirname "$JBOSS_CONSOLE_LOG")
  163.         chown $JBOSS_USER $(dirname "$JBOSS_PIDFILE") || true
  164.         cat /dev/null > "$JBOSS_CONSOLE_LOG"
  165.  
  166.         if [ "$JBOSS_MODE" = "standalone" ]; then
  167.             start-stop-daemon --start --user "$JBOSS_USER" \
  168.             --chuid "$JBOSS_USER" --chdir "$JBOSS_HOME" --pidfile "$JBOSS_PIDFILE" \
  169.             --exec "$JBOSS_SCRIPT" -- -c $JBOSS_CONFIG >> "$JBOSS_CONSOLE_LOG" 2>&1 &
  170.         else
  171.             start-stop-daemon --start --user "$JBOSS_USER" \
  172.             --chuid "$JBOSS_USER" --chdir "$JBOSS_HOME" --pidfile "$JBOSS_PIDFILE" \
  173.             --exec "$JBOSS_SCRIPT" -- --domain-config=$JBOSS_DOMAIN_CONFIG \
  174.             --host-config=$JBOSS_HOST_CONFIG >> "$JBOSS_CONSOLE_LOG" 2>&1 &
  175.         fi
  176.  
  177.         count=0
  178.         launched=0
  179.         until [ $count -gt $STARTUP_WAIT ]
  180.         do
  181. #           grep 'JBAS015874:' "$JBOSS_CONSOLE_LOG" > /dev/null
  182.             grep 'Listening for transport dt_socket' "$JBOSS_CONSOLE_LOG" > /dev/null
  183.             if [ $? -eq 0 ] ; then
  184.                 launched=1
  185.                 break
  186.             fi
  187.             sleep 1
  188.             count=$((count + 1));
  189.         done
  190.  
  191.         if check_status; then
  192.             log_end_msg 0
  193.         else
  194.             log_end_msg 1
  195.         fi
  196.  
  197.         if [ $launched -eq 0 ]; then
  198.             log_warning_msg "$DESC hasn't started within the timeout allowed"
  199.             log_warning_msg "please review file \"$JBOSS_CONSOLE_LOG\" to see the status of the service"
  200.         fi
  201.     elif [ $status_start -eq 1 ]; then
  202.         log_failure_msg "$DESC is not running but the pid file exists"
  203.         exit 1
  204.     elif [ $status_start -eq 0 ]; then
  205.         log_success_msg "$DESC (already running)"
  206.     fi
  207.  ;;
  208.  stop)
  209.     check_status
  210.     status_stop=$?
  211.     if [ $status_stop -eq 0 ]; then
  212.         read kpid < "$JBOSS_PIDFILE"
  213.         log_daemon_msg "Stopping $DESC" "$NAME"
  214.  
  215.         start-stop-daemon --start --chuid "$JBOSS_USER" \
  216.         --exec "$JBOSS_CLI" -- --connect --command=:shutdown \
  217.         >/dev/null 2>&1
  218.  
  219.         if [ $? -eq 1 ]; then
  220.             kill -15 $kpid
  221.         fi
  222.  
  223.         count=0
  224.         until [ $count -gt $SHUTDOWN_WAIT ]
  225.         do
  226.             check_status
  227.             if [ $? -eq 3 ]; then
  228.                 break
  229.             fi
  230.             sleep 1
  231.             count=$((count + 1));
  232.         done
  233.  
  234.         if [ $count -gt $SHUTDOWN_WAIT ]; then
  235.             kill -9 $kpid
  236.         fi
  237.        
  238.         log_end_msg 0
  239.     elif [ $status_stop -eq 1 ]; then
  240.         log_action_msg "$DESC is not running but the pid file exists, cleaning up"
  241.         rm -f $JBOSS_PIDFILE
  242.     elif [ $status_stop -eq 3 ]; then
  243.         log_action_msg "$DESC is not running"
  244.     fi
  245.  ;;
  246.  restart)
  247.     check_status
  248.     status_restart=$?
  249.     if [ $status_restart -eq 0 ]; then
  250.         $0 stop
  251.     fi
  252.     $0 start
  253.  ;;
  254.  reload|force-reload)
  255.     check_status
  256.     status_reload=$?
  257.     if [ $status_reload -eq 0 ]; then
  258.         log_daemon_msg "Reloading $DESC config" "$NAME"
  259.  
  260.         if [ "$JBOSS_MODE" = "standalone" ]; then
  261.             RELOAD_CMD=":reload"; else
  262.             RELOAD_CMD=":reload-servers"; fi
  263.  
  264.         start-stop-daemon --start --chuid "$JBOSS_USER" \
  265.         --exec "$JBOSS_CLI" -- --connect --command=$RELOAD_CMD >/dev/null 2>&1
  266.  
  267.         if [ $? -eq 0 ]; then
  268.             log_end_msg 0
  269.         else
  270.             log_end_msg 1
  271.         fi
  272.     else
  273.         log_failure_msg "$DESC is not running"
  274.     fi
  275.  ;;
  276.  status)
  277.     check_status
  278.     status=$?
  279.     if [ $status -eq 0 ]; then
  280.         read pid < $JBOSS_PIDFILE
  281.         log_action_msg "$DESC is running with pid $pid"
  282.         exit 0
  283.     elif [ $status -eq 1 ]; then
  284.         log_action_msg "$DESC is not running and the pid file exists"
  285.         exit 1
  286.     elif [ $status -eq 3 ]; then
  287.         log_action_msg "$DESC is not running"
  288.         exit 3
  289.     else
  290.         log_action_msg "Unable to determine $NAME status"
  291.         exit 4
  292.     fi
  293.  ;;
  294.  *)
  295.  log_action_msg "Usage: $0 {start|stop|restart|reload|force-reload|status}"
  296.  exit 2
  297.  ;;
  298. esac
  299.  
  300. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement