Created
October 14, 2025 03:36
-
-
Save hkneptune/e7b1a73675dda8415e56ab65ee5e432f to your computer and use it in GitHub Desktop.
WildFly Startup Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # WildFly Startup Script | |
| # Configuration variables | |
| WILDFLY_HOME="/opt/wildfly-12.0.0.Final" | |
| STANDALONE_DIR="standalone" | |
| BIND_ADDRESS="0.0.0.0" | |
| WILDFLY_BIN="${WILDFLY_HOME}/bin" | |
| STANDALONE_FULL_PATH="${WILDFLY_HOME}/${STANDALONE_DIR}" | |
| # Colors for output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| NC='\033[0m' # No Color | |
| # Function to print colored output | |
| print_status() { | |
| echo -e "${GREEN}[INFO]${NC} $1" | |
| } | |
| print_warning() { | |
| echo -e "${YELLOW}[WARNING]${NC} $1" | |
| } | |
| print_error() { | |
| echo -e "${RED}[ERROR]${NC} $1" | |
| } | |
| # Function to check if WildFly is already running | |
| check_running_process() { | |
| local pid=$(ps aux | grep "[j]boss" | grep -v grep | awk '{print $2}') | |
| if [ ! -z "$pid" ]; then | |
| print_warning "WildFly process with PID $pid is already running" | |
| return 0 | |
| else | |
| return 1 | |
| fi | |
| } | |
| # Function to validate WildFly installation | |
| validate_installation() { | |
| print_status "Validating WildFly installation..." | |
| if [ ! -d "$WILDFLY_HOME" ]; then | |
| print_error "WildFly directory not found: $WILDFLY_HOME" | |
| print_error "Please ensure WildFly 12 is installed in /opt/wildfly" | |
| exit 1 | |
| fi | |
| if [ ! -d "${WILDFLY_BIN}" ]; then | |
| print_error "WildFly bin directory not found: ${WILDFLY_BIN}" | |
| exit 1 | |
| fi | |
| if [ ! -f "${WILDFLY_BIN}/standalone.sh" ]; then | |
| print_error "standalone.sh not found in ${WILDFLY_BIN}" | |
| exit 1 | |
| fi | |
| if [ ! -d "$STANDALONE_FULL_PATH" ]; then | |
| print_warning "Custom standalone directory doesn't exist: $STANDALONE_FULL_PATH" | |
| print_status "Creating standalone directory..." | |
| mkdir -p "$STANDALONE_FULL_PATH" | |
| # Copy default configuration if it doesn't exist | |
| if [ ! -f "$STANDALONE_FULL_PATH/configuration/standalone.xml" ]; then | |
| print_status "Copying default configuration to custom standalone directory..." | |
| cp -r "${WILDFLY_HOME}/standalone"/* "$STANDALONE_FULL_PATH/" 2>/dev/null || { | |
| print_error "Failed to copy default configuration" | |
| exit 1 | |
| } | |
| fi | |
| fi | |
| print_status "WildFly installation validated successfully" | |
| } | |
| # Function to configure standalone.xml for binding to all interfaces | |
| configure_network_bindings() { | |
| local config_file="$STANDALONE_FULL_PATH/configuration/standalone.xml" | |
| print_status "Configuring network bindings to allow all traffic (0.0.0.0)..." | |
| # Create a backup of the original configuration | |
| if [ ! -f "${config_file}.backup" ]; then | |
| cp "$config_file" "${config_file}.backup" | |
| print_status "Created backup of configuration: ${config_file}.backup" | |
| fi | |
| # Update socket-binding-group to bind to 0.0.0.0 | |
| if grep -q 'socket-binding-group' "$config_file"; then | |
| # Update the default interface to management and public to bind to 0.0.0.0 | |
| sed -i "s|<socket-binding-group name=\"standard-sockets\" default-interface=\"management\" port-offset=\"\${jboss.socket.binding.port-offset:0}\">|<socket-binding-group name=\"standard-sockets\" default-interface=\"public\" port-offset=\"\${jboss.socket.binding.port-offset:0}\">|" "$config_file" | |
| # Update interface definitions | |
| cat >> "$config_file" << 'EOF' | |
| <!-- Update interface definitions to bind to all interfaces --> | |
| <interfaces> | |
| <interface name="management"> | |
| <inet-address value="${jboss.bind.address.management:0.0.0.0}"/> | |
| </interface> | |
| <interface name="public"> | |
| <inet-address value="${jboss.bind.address:0.0.0.0}"/> | |
| </interface> | |
| </interfaces> | |
| EOF | |
| print_status "Network bindings updated to allow all traffic from 0.0.0.0" | |
| else | |
| print_warning "Could not find socket-binding-group configuration. Manual configuration may be required." | |
| fi | |
| } | |
| # Function to set environment variables | |
| set_environment() { | |
| print_status "Setting up environment variables..." | |
| # Add WildFly bin to PATH | |
| export PATH="$WILDFLY_BIN:$PATH" | |
| # Set JBOSS_HOME | |
| export JBOSS_HOME="$WILDFLY_HOME" | |
| # Set standalone directory | |
| export JBOSS_BASE_DIR="$STANDALONE_FULL_PATH" | |
| # Set binding addresses | |
| export JBOSS_BIND_ADDRESS="$BIND_ADDRESS" | |
| export JBOSS_BIND_ADDRESS_MANAGEMENT="$BIND_ADDRESS" | |
| print_status "Environment variables configured" | |
| } | |
| # Function to start WildFly | |
| start_wildfly() { | |
| print_status "Starting WildFly 12 with custom standalone directory: $STANDALONE_DIR" | |
| print_status "Binding to: $BIND_ADDRESS (all interfaces allowed)" | |
| # Check if already running | |
| if check_running_process; then | |
| print_warning "WildFly appears to be already running. Please stop the existing instance first." | |
| exit 1 | |
| fi | |
| # Start WildFly in standalone mode | |
| cd "$WILDFLY_BIN" | |
| # Use nohup to run in background and redirect output | |
| nohup ./standalone.sh \ | |
| -b "$BIND_ADDRESS" \ | |
| -bmanagement "$BIND_ADDRESS" \ | |
| --server-config=standalone.xml 2>&1 > "$STANDALONE_FULL_PATH/log/wildfly.log" & | |
| local pid=$! | |
| print_status "WildFly started with PID: $pid" | |
| print_status "Log output redirected to: $STANDALONE_FULL_PATH/log/wildfly.log" | |
| # Wait a moment and check if process is still running | |
| sleep 3 | |
| if kill -0 $pid 2>/dev/null; then | |
| print_status "WildFly startup successful!" | |
| print_status "Access WildFly Admin Console at: http://$BIND_ADDRESS:9990" | |
| print_status "Application server available at: http://$BIND_ADDRESS:8080" | |
| echo | |
| print_status "To stop WildFly, run: ./stop-wildfly.sh" | |
| else | |
| print_error "WildFly failed to start. Check logs at: $STANDALONE_FULL_PATH/log/wildfly.log" | |
| exit 1 | |
| fi | |
| } | |
| # Function to display usage | |
| show_usage() { | |
| echo "WildFly 12 Startup Script" | |
| echo "Usage: $0 [start|stop|status|restart|configure]" | |
| echo | |
| echo "Commands:" | |
| echo " start - Start WildFly with custom standalone directory" | |
| echo " stop - Stop WildFly" | |
| echo " status - Check WildFly status" | |
| echo " restart - Restart WildFly" | |
| echo " configure - Configure network bindings only (no start)" | |
| echo | |
| echo "Configuration:" | |
| echo " WildFly Home: $WILDFLY_HOME" | |
| echo " Standalone Dir: $STANDALONE_DIR" | |
| echo " Bind Address: $BIND_ADDRESS" | |
| echo | |
| exit 1 | |
| } | |
| # Function to stop WildFly | |
| stop_wildfly() { | |
| print_status "Stopping WildFly..." | |
| local pid=$(ps aux | grep "[j]boss" | grep -v grep | awk '{print $2}') | |
| if [ -z "$pid" ]; then | |
| print_status "WildFly is not running" | |
| exit 0 | |
| fi | |
| # Try graceful shutdown first | |
| if [ -f "$WILDFLY_BIN/jboss-cli.sh" ]; then | |
| print_status "Attempting graceful shutdown via CLI..." | |
| "$WILDFLY_BIN/jboss-cli.sh" --connect --command=:shutdown 2>/dev/null | |
| sleep 5 | |
| fi | |
| # Force kill if still running | |
| if kill -0 $pid 2>/dev/null; then | |
| print_status "Force stopping WildFly process (PID: $pid)..." | |
| kill -TERM $pid | |
| sleep 3 | |
| if kill -0 $pid 2>/dev/null; then | |
| print_warning "Process still running, using SIGKILL..." | |
| kill -KILL $pid | |
| fi | |
| fi | |
| print_status "WildFly stopped successfully" | |
| } | |
| # Function to check status | |
| check_status() { | |
| local pid=$(ps aux | grep "[j]boss" | grep -v grep | awk '{print $2}') | |
| if [ -z "$pid" ]; then | |
| print_status "WildFly is NOT running" | |
| exit 1 | |
| else | |
| print_status "WildFly is RUNNING (PID: $pid)" | |
| print_status "Log file: $STANDALONE_FULL_PATH/log/wildfly.log" | |
| exit 0 | |
| fi | |
| } | |
| # Function to restart WildFly | |
| restart_wildfly() { | |
| print_status "Restarting WildFly..." | |
| stop_wildfly | |
| sleep 3 | |
| start_wildfly | |
| } | |
| # Main script logic | |
| main() { | |
| case "$1" in | |
| start) | |
| validate_installation | |
| set_environment | |
| start_wildfly | |
| ;; | |
| stop) | |
| stop_wildfly | |
| ;; | |
| status) | |
| check_status | |
| ;; | |
| restart) | |
| restart_wildfly | |
| ;; | |
| configure) | |
| validate_installation | |
| configure_network_bindings | |
| print_status "Configuration completed. Use 'start' to launch WildFly." | |
| ;; | |
| *) | |
| show_usage | |
| ;; | |
| esac | |
| } | |
| # Ensure script is executable and handle arguments | |
| if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then | |
| main "$@" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment