Keeping script alive + better logs and sigterm

This commit is contained in:
2025-09-24 12:16:25 +02:00
parent b3b45380a2
commit 7adc49f5b0

View File

@@ -4,7 +4,18 @@ set -e
# Set default values if not provided
TARGET_HOST=${TARGET_HOST}
TARGET_PORT=${TARGET_PORT}
UNIX_SOCKET_PATH=${UNIX_SOCKET_PATH}
UNIX_SOCKET_PATH=${UNIX_SOCKET_PATH:-/socket/docker.sock}
# Validate required environment variables
if [ -z "$TARGET_HOST" ]; then
echo "ERROR: TARGET_HOST environment variable is required"
exit 1
fi
if [ -z "$TARGET_PORT" ]; then
echo "ERROR: TARGET_PORT environment variable is required"
exit 1
fi
echo "Starting socat proxy..."
echo "UNIX socket: $UNIX_SOCKET_PATH"
@@ -24,6 +35,40 @@ echo "Creating socket with netcat..."
# Create socket with nc -lU in background and then kill it to create the socket file
timeout 1 nc -lU "$UNIX_SOCKET_PATH" || true
# Execute socat to proxy UNIX socket to TCP
exec socat UNIX-LISTEN:$UNIX_SOCKET_PATH,fork,unlink-early TCP:$TARGET_HOST:$TARGET_PORT
echo "Testing connection to target..."
# Test if we can reach the target before starting socat
if ! nc -z "$TARGET_HOST" "$TARGET_PORT" 2>/dev/null; then
echo "WARNING: Cannot connect to $TARGET_HOST:$TARGET_PORT - socat will retry automatically"
fi
# Signal handler for graceful shutdown
cleanup() {
echo "Received SIGTERM, shutting down gracefully..."
if [ ! -z "$SOCAT_PID" ]; then
echo "Stopping socat process (PID: $SOCAT_PID)..."
kill "$SOCAT_PID" 2>/dev/null || true
wait "$SOCAT_PID" 2>/dev/null || true
fi
echo "Cleanup completed, exiting..."
exit 0
}
# Set up signal trap
trap cleanup SIGTERM SIGINT
echo "Starting socat proxy..."
# Start socat with verbose logging and redirect to stdout/stderr
socat -d -d UNIX-LISTEN:$UNIX_SOCKET_PATH,fork,unlink-early TCP:$TARGET_HOST:$TARGET_PORT &
SOCAT_PID=$!
echo "Socat started with PID: $SOCAT_PID"
echo "Container is ready and running..."
# Keep the script alive and wait for socat process
while kill -0 "$SOCAT_PID" 2>/dev/null; do
sleep 1
done
echo "Socat process has stopped"
exit 1