#!/usr/bin/env bash # Run mirror configuration early, before any apt/pip operations if [[ "$USE_CHINA_MIRRORS" == "1" || "${USE_CHINA_MIRRORS,,}" == "true" ]]; then /bin/configure-china-mirrors fi # Update apt package cache if requested if [[ "$UPDATE_APT" == "1" || "${UPDATE_APT,,}" == "true" ]]; then /bin/update-apt-cache fi # Global variables to track background processes TAIL_PID="" INIT_PID="" term_handler() { echo "Received termination signal (PID: $$), cleaning up..." # First run user-defined exit commands (may gracefully shut down processes) if [[ -n "$COMMAND_EXIT" ]]; then echo "Running legacy COMMAND_EXIT..." eval "$COMMAND_EXIT" fi # Then run numbered COMMAND_EXIT variables in order for i in {01..99}; do var_name="COMMAND_EXIT$i" var_value="${!var_name}" if [[ -n "$var_value" ]]; then echo "Running $var_name..." eval "$var_value" fi done # Kill any still-running background processes if they didn't shut down gracefully if [[ -n "$INIT_PID" ]]; then if kill -0 "$INIT_PID" 2>/dev/null; then echo "Init process still running, force killing: $INIT_PID" kill "$INIT_PID" 2>/dev/null else echo "Init process already terminated gracefully" fi fi # Kill the tail process if it's running if [[ -n "$TAIL_PID" ]]; then echo "Killing tail process: $TAIL_PID" kill "$TAIL_PID" 2>/dev/null fi # Stop SSH service echo "Stopping SSH service..." sudo service ssh stop 2>/dev/null || true echo "Cleanup completed, exiting..." exit 0 } # Debug: Show our PID and signal handling setup echo "Entrypoint script starting with PID: $$" echo "Setting up signal handlers..." # Setup signal handlers for graceful shutdown trap 'term_handler' SIGTERM SIGINT SIGQUIT # Test signal handling is working echo "Signal handlers registered. Testing with kill -0..." # Setup home directory for the current user /bin/setup-user-home if [[ $LOG_FILE != "/dev/null" ]]; then sudo touch "$LOG_FILE" sudo chown -R "$(id -u):$(id -g)" "$LOG_FILE" fi echo "Starting SSH service..." sudo service ssh start # Run initialization commands (last one in background to allow signal handling) # First collect all defined COMMAND_INIT variables init_commands=() if [[ -n "$COMMAND_INIT" ]]; then init_commands+=("COMMAND_INIT") fi for i in {01..99}; do var_name="COMMAND_INIT$i" if [[ -n "${!var_name}" ]]; then init_commands+=("$var_name") fi done # Execute them: all synchronously except the last one for ((i=0; i<${#init_commands[@]}; i++)); do var_name="${init_commands[i]}" var_value="${!var_name}" if [[ $i -eq $((${#init_commands[@]} - 1)) ]]; then # Last command - run in background to avoid blocking signals echo "Running $var_name (final command) in background..." eval "$var_value" & INIT_PID=$! echo "Background process started with PID: $INIT_PID" else # Not last - run synchronously to preserve expected behavior echo "Running $var_name..." eval "$var_value" fi done # Start the main process loop if [[ $LOG_FILE == "/dev/null" ]]; then # If no log file, just wait for signals echo "Container ready, waiting for signals..." while true; do sleep 1 & wait $! || break done else # If log file specified, tail it echo "Container ready, tailing log file: $LOG_FILE" tail -f "$LOG_FILE" & TAIL_PID=$! # Wait for the tail process or signals, but make it interruptible while kill -0 $TAIL_PID 2>/dev/null; do sleep 1 & wait $! || break done fi # This should never be reached, but if it is, exit gracefully echo "Main loop exited unexpectedly" exit 0