Fixed the issue with mis handling of term signal. Now the long running process is automatically send to background.

This commit is contained in:
Ameer Hamza Khan
2025-09-11 10:14:57 +00:00
parent d6837c0329
commit 574f6825be

View File

@@ -16,20 +16,16 @@ if [[ "$UPDATE_APT" == "1" || "${UPDATE_APT,,}" == "true" ]]; then
fi
fi
# Global variable to track background processes
# Global variables to track background processes
TAIL_PID=""
INIT_PID=""
term_handler() {
echo "Received termination signal, cleaning up..."
echo "Received termination signal (PID: $$), cleaning up..."
# Kill the tail process if it's running
if [[ -n "$TAIL_PID" ]]; then
kill "$TAIL_PID" 2>/dev/null
fi
# Run user-defined exit commands
# First run the legacy COMMAND_EXIT for backward compatibility
# 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
@@ -43,12 +39,39 @@ term_handler() {
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
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)
@@ -65,17 +88,32 @@ fi
echo "Starting SSH service..."
sudo service ssh start
# Run initialization commands
# First run the legacy COMMAND_INIT for backward compatibility
# 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
eval "$COMMAND_INIT"
init_commands+=("COMMAND_INIT")
fi
# Then run numbered COMMAND_INIT variables in order
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 [[ -n "$var_value" ]]; then
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
@@ -87,7 +125,7 @@ if [[ $LOG_FILE == "/dev/null" ]]; then
echo "Container ready, waiting for signals..."
while true; do
sleep 1 &
wait $!
wait $! || break
done
else
# If log file specified, tail it
@@ -95,6 +133,13 @@ else
tail -f "$LOG_FILE" &
TAIL_PID=$!
# Wait for the tail process or signals
wait $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