#!/usr/bin/env bash if [[ "$UPDATE_APT" == "1" || "${UPDATE_APT,,}" == "true" ]]; then # Gracefully update package lists if network is available echo "Checking network connectivity..." if ping -c 1 -W 5 8.8.8.8 >/dev/null 2>&1 || ping -c 1 -W 5 1.1.1.1 >/dev/null 2>&1; then echo "Network detected, refreshing package lists..." if sudo apt update; then echo "Package lists updated successfully" else echo "Warning: apt update failed despite network connectivity" echo "Falling back to cached package lists" fi else echo "No network connectivity detected, using cached package lists" fi 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. It is useful for attaching vscode with container. user_name=$(whoami) user_home="/home/$user_name" sudo mkdir -p "$user_home" sudo chown -R "$(id -u):$(id -g)" "$user_home" cp -r /etc/skel/. "$user_home" 2>/dev/null || true 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