Fixed the issue with mis handling of term signal. Now the long running process is automatically send to background.
This commit is contained in:
@@ -16,20 +16,16 @@ if [[ "$UPDATE_APT" == "1" || "${UPDATE_APT,,}" == "true" ]]; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Global variable to track background processes
|
# Global variables to track background processes
|
||||||
TAIL_PID=""
|
TAIL_PID=""
|
||||||
|
INIT_PID=""
|
||||||
|
|
||||||
term_handler() {
|
term_handler() {
|
||||||
echo "Received termination signal, cleaning up..."
|
echo "Received termination signal (PID: $$), cleaning up..."
|
||||||
|
|
||||||
# Kill the tail process if it's running
|
# First run user-defined exit commands (may gracefully shut down processes)
|
||||||
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
|
|
||||||
if [[ -n "$COMMAND_EXIT" ]]; then
|
if [[ -n "$COMMAND_EXIT" ]]; then
|
||||||
|
echo "Running legacy COMMAND_EXIT..."
|
||||||
eval "$COMMAND_EXIT"
|
eval "$COMMAND_EXIT"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -43,12 +39,39 @@ term_handler() {
|
|||||||
fi
|
fi
|
||||||
done
|
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..."
|
echo "Cleanup completed, exiting..."
|
||||||
exit 0
|
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
|
# 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.
|
# setup home directory for the current user. It is useful for attaching vscode with container.
|
||||||
user_name=$(whoami)
|
user_name=$(whoami)
|
||||||
@@ -65,17 +88,32 @@ fi
|
|||||||
echo "Starting SSH service..."
|
echo "Starting SSH service..."
|
||||||
sudo service ssh start
|
sudo service ssh start
|
||||||
|
|
||||||
# Run initialization commands
|
# Run initialization commands (last one in background to allow signal handling)
|
||||||
# First run the legacy COMMAND_INIT for backward compatibility
|
# First collect all defined COMMAND_INIT variables
|
||||||
|
init_commands=()
|
||||||
if [[ -n "$COMMAND_INIT" ]]; then
|
if [[ -n "$COMMAND_INIT" ]]; then
|
||||||
eval "$COMMAND_INIT"
|
init_commands+=("COMMAND_INIT")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Then run numbered COMMAND_INIT variables in order
|
|
||||||
for i in {01..99}; do
|
for i in {01..99}; do
|
||||||
var_name="COMMAND_INIT$i"
|
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}"
|
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..."
|
echo "Running $var_name..."
|
||||||
eval "$var_value"
|
eval "$var_value"
|
||||||
fi
|
fi
|
||||||
@@ -87,7 +125,7 @@ if [[ $LOG_FILE == "/dev/null" ]]; then
|
|||||||
echo "Container ready, waiting for signals..."
|
echo "Container ready, waiting for signals..."
|
||||||
while true; do
|
while true; do
|
||||||
sleep 1 &
|
sleep 1 &
|
||||||
wait $!
|
wait $! || break
|
||||||
done
|
done
|
||||||
else
|
else
|
||||||
# If log file specified, tail it
|
# If log file specified, tail it
|
||||||
@@ -95,6 +133,13 @@ else
|
|||||||
tail -f "$LOG_FILE" &
|
tail -f "$LOG_FILE" &
|
||||||
TAIL_PID=$!
|
TAIL_PID=$!
|
||||||
|
|
||||||
# Wait for the tail process or signals
|
# Wait for the tail process or signals, but make it interruptible
|
||||||
wait $TAIL_PID
|
while kill -0 $TAIL_PID 2>/dev/null; do
|
||||||
|
sleep 1 &
|
||||||
|
wait $! || break
|
||||||
|
done
|
||||||
fi
|
fi
|
||||||
|
# This should never be reached, but if it is, exit gracefully
|
||||||
|
echo "Main loop exited unexpectedly"
|
||||||
|
exit 0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user