@@ -308,22 +308,39 @@ func TestGenerateScriptWithTimeout(t *testing.T) {
308308export POSTSTART_TIMEOUT_DURATION="10"
309309export POSTSTART_KILL_AFTER_DURATION="5"
310310
311- echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
311+ _TIMEOUT_COMMAND_PART=""
312+ _WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean
312313
313- # Run the user's script under the 'timeout' utility.
314- timeout --preserve-status --kill-after="${POSTSTART_KILL_AFTER_DURATION}" "${POSTSTART_TIMEOUT_DURATION}" /bin/sh -c 'echo 'hello world'
314+ if command -v timeout >/dev/null 2>&1; then
315+ echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
316+ _TIMEOUT_COMMAND_PART="timeout --preserve-status --kill-after=\"${POSTSTART_KILL_AFTER_DURATION}\" \"${POSTSTART_TIMEOUT_DURATION}\""
317+ _WAS_TIMEOUT_USED="true"
318+ else
319+ echo "[postStart hook] WARNING: 'timeout' utility not found. Executing commands without timeout." >&2
320+ fi
321+
322+ # Execute the user's script
323+ ${_TIMEOUT_COMMAND_PART} /bin/sh -c 'echo 'hello world'
315324sleep 1'
316325exit_code=$?
317326
318- # Check the exit code from 'timeout'
319- if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
320- echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
321- elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
322- echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
323- elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
324- echo "[postStart hook] Commands failed with exit code $exit_code." >&2
327+ # Check the exit code based on whether timeout was attempted
328+ if [ "$_WAS_TIMEOUT_USED" = "true" ]; then
329+ if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
330+ echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
331+ elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
332+ echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
333+ elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
334+ echo "[postStart hook] Commands failed with exit code $exit_code." >&2
335+ else
336+ echo "[postStart hook] Commands completed successfully within the time limit." >&2
337+ fi
325338else
326- echo "[postStart hook] Commands completed successfully within the time limit." >&2
339+ if [ $exit_code -ne 0 ]; then
340+ echo "[postStart hook] Commands failed with exit code $exit_code (no timeout)." >&2
341+ else
342+ echo "[postStart hook] Commands completed successfully (no timeout)." >&2
343+ fi
327344fi
328345
329346exit $exit_code
@@ -337,21 +354,38 @@ exit $exit_code
337354export POSTSTART_TIMEOUT_DURATION="0"
338355export POSTSTART_KILL_AFTER_DURATION="5"
339356
340- echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
357+ _TIMEOUT_COMMAND_PART=""
358+ _WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean
359+
360+ if command -v timeout >/dev/null 2>&1; then
361+ echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
362+ _TIMEOUT_COMMAND_PART="timeout --preserve-status --kill-after=\"${POSTSTART_KILL_AFTER_DURATION}\" \"${POSTSTART_TIMEOUT_DURATION}\""
363+ _WAS_TIMEOUT_USED="true"
364+ else
365+ echo "[postStart hook] WARNING: 'timeout' utility not found. Executing commands without timeout." >&2
366+ fi
341367
342- # Run the user's script under the 'timeout' utility.
343- timeout --preserve-status --kill-after="${POSTSTART_KILL_AFTER_DURATION}" "${POSTSTART_TIMEOUT_DURATION}" /bin/sh -c 'echo 'running indefinitely...''
368+ # Execute the user's script
369+ ${_TIMEOUT_COMMAND_PART} /bin/sh -c 'echo 'running indefinitely...''
344370exit_code=$?
345371
346- # Check the exit code from 'timeout'
347- if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
348- echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
349- elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
350- echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
351- elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
352- echo "[postStart hook] Commands failed with exit code $exit_code." >&2
372+ # Check the exit code based on whether timeout was attempted
373+ if [ "$_WAS_TIMEOUT_USED" = "true" ]; then
374+ if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
375+ echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
376+ elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
377+ echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
378+ elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
379+ echo "[postStart hook] Commands failed with exit code $exit_code." >&2
380+ else
381+ echo "[postStart hook] Commands completed successfully within the time limit." >&2
382+ fi
353383else
354- echo "[postStart hook] Commands completed successfully within the time limit." >&2
384+ if [ $exit_code -ne 0 ]; then
385+ echo "[postStart hook] Commands failed with exit code $exit_code (no timeout)." >&2
386+ else
387+ echo "[postStart hook] Commands completed successfully (no timeout)." >&2
388+ fi
355389fi
356390
357391exit $exit_code
@@ -365,21 +399,38 @@ exit $exit_code
365399export POSTSTART_TIMEOUT_DURATION="5"
366400export POSTSTART_KILL_AFTER_DURATION="5"
367401
368- echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
402+ _TIMEOUT_COMMAND_PART=""
403+ _WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean
369404
370- # Run the user's script under the 'timeout' utility.
371- timeout --preserve-status --kill-after="${POSTSTART_KILL_AFTER_DURATION}" "${POSTSTART_TIMEOUT_DURATION}" /bin/sh -c ''
405+ if command -v timeout >/dev/null 2>&1; then
406+ echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
407+ _TIMEOUT_COMMAND_PART="timeout --preserve-status --kill-after=\"${POSTSTART_KILL_AFTER_DURATION}\" \"${POSTSTART_TIMEOUT_DURATION}\""
408+ _WAS_TIMEOUT_USED="true"
409+ else
410+ echo "[postStart hook] WARNING: 'timeout' utility not found. Executing commands without timeout." >&2
411+ fi
412+
413+ # Execute the user's script
414+ ${_TIMEOUT_COMMAND_PART} /bin/sh -c ''
372415exit_code=$?
373416
374- # Check the exit code from 'timeout'
375- if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
376- echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
377- elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
378- echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
379- elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
380- echo "[postStart hook] Commands failed with exit code $exit_code." >&2
417+ # Check the exit code based on whether timeout was attempted
418+ if [ "$_WAS_TIMEOUT_USED" = "true" ]; then
419+ if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
420+ echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
421+ elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
422+ echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
423+ elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
424+ echo "[postStart hook] Commands failed with exit code $exit_code." >&2
425+ else
426+ echo "[postStart hook] Commands completed successfully within the time limit." >&2
427+ fi
381428else
382- echo "[postStart hook] Commands completed successfully within the time limit." >&2
429+ if [ $exit_code -ne 0 ]; then
430+ echo "[postStart hook] Commands failed with exit code $exit_code (no timeout)." >&2
431+ else
432+ echo "[postStart hook] Commands completed successfully (no timeout)." >&2
433+ fi
383434fi
384435
385436exit $exit_code
@@ -393,21 +444,38 @@ exit $exit_code
393444export POSTSTART_TIMEOUT_DURATION="30"
394445export POSTSTART_KILL_AFTER_DURATION="5"
395446
396- echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
447+ _TIMEOUT_COMMAND_PART=""
448+ _WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean
449+
450+ if command -v timeout >/dev/null 2>&1; then
451+ echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
452+ _TIMEOUT_COMMAND_PART="timeout --preserve-status --kill-after=\"${POSTSTART_KILL_AFTER_DURATION}\" \"${POSTSTART_TIMEOUT_DURATION}\""
453+ _WAS_TIMEOUT_USED="true"
454+ else
455+ echo "[postStart hook] WARNING: 'timeout' utility not found. Executing commands without timeout." >&2
456+ fi
397457
398- # Run the user's script under the 'timeout' utility.
399- timeout --preserve-status --kill-after="${POSTSTART_KILL_AFTER_DURATION}" "${POSTSTART_TIMEOUT_DURATION}" /bin/sh -c 'echo 'it'\''s complex''
458+ # Execute the user's script
459+ ${_TIMEOUT_COMMAND_PART} /bin/sh -c 'echo 'it'\''s complex''
400460exit_code=$?
401461
402- # Check the exit code from 'timeout'
403- if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
404- echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
405- elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
406- echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
407- elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
408- echo "[postStart hook] Commands failed with exit code $exit_code." >&2
462+ # Check the exit code based on whether timeout was attempted
463+ if [ "$_WAS_TIMEOUT_USED" = "true" ]; then
464+ if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
465+ echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
466+ elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
467+ echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
468+ elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
469+ echo "[postStart hook] Commands failed with exit code $exit_code." >&2
470+ else
471+ echo "[postStart hook] Commands completed successfully within the time limit." >&2
472+ fi
409473else
410- echo "[postStart hook] Commands completed successfully within the time limit." >&2
474+ if [ $exit_code -ne 0 ]; then
475+ echo "[postStart hook] Commands failed with exit code $exit_code (no timeout)." >&2
476+ else
477+ echo "[postStart hook] Commands completed successfully (no timeout)." >&2
478+ fi
411479fi
412480
413481exit $exit_code
0 commit comments