@@ -108,26 +108,44 @@ func buildUserScript(commands []dw.Command) (string, error) {
108108// generateScriptWithTimeout wraps a given user script with timeout logic,
109109// environment variable exports, and specific exit code handling.
110110// The killAfterDurationSeconds is hardcoded to 5s within this generated script.
111+ // It conditionally prefixes the user script with the timeout command if available.
111112func generateScriptWithTimeout (escapedUserScript string , timeoutSeconds int32 ) string {
112113 return fmt .Sprintf (`
113114export POSTSTART_TIMEOUT_DURATION="%d"
114115export POSTSTART_KILL_AFTER_DURATION="5"
115116
116- echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
117+ _TIMEOUT_COMMAND_PART=""
118+ _WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean
117119
118- # Run the user's script under the 'timeout' utility.
119- timeout --preserve-status --kill-after="${POSTSTART_KILL_AFTER_DURATION}" "${POSTSTART_TIMEOUT_DURATION}" /bin/sh -c '%s'
120+ if command -v timeout >/dev/null 2>&1; then
121+ echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
122+ _TIMEOUT_COMMAND_PART="timeout --preserve-status --kill-after=\"${POSTSTART_KILL_AFTER_DURATION}\" \"${POSTSTART_TIMEOUT_DURATION}\""
123+ _WAS_TIMEOUT_USED="true"
124+ else
125+ echo "[postStart hook] WARNING: 'timeout' utility not found. Executing commands without timeout." >&2
126+ fi
127+
128+ # Execute the user's script
129+ ${_TIMEOUT_COMMAND_PART} /bin/sh -c '%s'
120130exit_code=$?
121131
122- # Check the exit code from 'timeout'
123- if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
124- echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
125- elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
126- echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
127- elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
128- echo "[postStart hook] Commands failed with exit code $exit_code." >&2
132+ # Check the exit code based on whether timeout was attempted
133+ if [ "$_WAS_TIMEOUT_USED" = "true" ]; then
134+ if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
135+ echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
136+ elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
137+ echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
138+ elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
139+ echo "[postStart hook] Commands failed with exit code $exit_code." >&2
140+ else
141+ echo "[postStart hook] Commands completed successfully within the time limit." >&2
142+ fi
129143else
130- echo "[postStart hook] Commands completed successfully within the time limit." >&2
144+ if [ $exit_code -ne 0 ]; then
145+ echo "[postStart hook] Commands failed with exit code $exit_code (no timeout)." >&2
146+ else
147+ echo "[postStart hook] Commands completed successfully (no timeout)." >&2
148+ fi
131149fi
132150
133151exit $exit_code
0 commit comments