From 2159e34ebe1060431b8887349a1968883979c0a6 Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Thu, 5 Dec 2024 13:24:26 +0100 Subject: [PATCH] Use pgrep if available --- .templates/ha_entrypoint.sh | 42 ++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/.templates/ha_entrypoint.sh b/.templates/ha_entrypoint.sh index eb174549c..ae15d185e 100755 --- a/.templates/ha_entrypoint.sh +++ b/.templates/ha_entrypoint.sh @@ -12,7 +12,7 @@ for SCRIPTS in /etc/cont-init.d/*; do echo "$SCRIPTS: executing" # Check if run as root - if test "$(id -u)" == 0 && test "$(id -u)" == 0; then + if [ "$(id -u)" -eq 0 ]; then chown "$(id -u)":"$(id -g)" "$SCRIPTS" chmod a+x "$SCRIPTS" else @@ -27,20 +27,21 @@ for SCRIPTS in /etc/cont-init.d/*; do # Get current shebang, if not available use another currentshebang="$(sed -n '1{s/^#![[:blank:]]*//p;q}' "$SCRIPTS")" if [ ! -f "${currentshebang%% *}" ]; then - for shebang in "/command/with-contenv bashio" "/usr/bin/env bashio" "/usr/bin/bashio" "/bin/bash" "/bin/sh"; do if [ -f "${shebang%% *}" ]; then break; fi; done + for shebang in "/command/with-contenv bashio" "/usr/bin/env bashio" "/usr/bin/bashio" "/bin/bash" "/bin/sh"; do + if [ -f "${shebang%% *}" ]; then + break + fi + done sed -i "s|$currentshebang|$shebang|g" "$SCRIPTS" fi # Use source to share env variables when requested if [ "${ha_entry_source:-null}" = true ] && command -v "source" &>/dev/null; then - # Exit cannot be used with source sed -i "s/(.*\s|^)exit ([0-9]+)/\1 return \2 || exit \2/g" "$SCRIPTS" sed -i "s/bashio::exit.nok/return 1/g" "$SCRIPTS" sed -i "s/bashio::exit.ok/return 0/g" "$SCRIPTS" - # shellcheck source=/dev/null source "$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?" else - # Support for posix only shell "$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?" fi @@ -57,24 +58,31 @@ if [ "$$" -eq 1 ]; then echo -e "\033[0;32mEverything started!\033[0m" terminate() { echo "Termination signal received, forwarding to subprocesses..." + + if command -v pgrep &>/dev/null; then + # Use pgrep if available to find child processes + for pid in $(pgrep -P $$); do + echo "Terminating child PID $pid" + kill -TERM "$pid" 2>/dev/null || echo "Failed to terminate PID $pid" + done + else + # Fallback to iterating through /proc + for pid in /proc/[0-9]*/; do + pid=${pid#/proc/} + pid=${pid%/} + if [[ "$pid" -ne 1 ]] && grep -q "^PPid:\s*$$" "/proc/$pid/status" 2>/dev/null; then + echo "Terminating child PID $pid" + kill -TERM "$pid" 2>/dev/null || echo "Failed to terminate PID $pid" + fi + done + fi - # Gracefully terminate open subprocesses - for pid in /proc/[0-9]*/; do - pid=${pid#/proc/} - pid=${pid%/} - if [[ "$pid" -ne 1 ]]; then - echo "Terminating PID $pid" - kill -TERM "$pid" 2>/dev/null || echo "Failed to terminate PID $pid" - fi - done - - # Wait for all child processes to terminate wait echo "All subprocesses terminated. Exiting." exit 0 } trap terminate SIGTERM SIGINT - while true; do sleep 86400 & wait $!; done + while :; do sleep infinity & wait $!; done else echo " " echo -e "\033[0;32mStarting the upstream container\033[0m"