mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-07-24 23:10:32 +02:00
Use pgrep if available
This commit is contained in:
@@ -12,7 +12,7 @@ for SCRIPTS in /etc/cont-init.d/*; do
|
|||||||
echo "$SCRIPTS: executing"
|
echo "$SCRIPTS: executing"
|
||||||
|
|
||||||
# Check if run as root
|
# 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"
|
chown "$(id -u)":"$(id -g)" "$SCRIPTS"
|
||||||
chmod a+x "$SCRIPTS"
|
chmod a+x "$SCRIPTS"
|
||||||
else
|
else
|
||||||
@@ -27,20 +27,21 @@ for SCRIPTS in /etc/cont-init.d/*; do
|
|||||||
# Get current shebang, if not available use another
|
# Get current shebang, if not available use another
|
||||||
currentshebang="$(sed -n '1{s/^#![[:blank:]]*//p;q}' "$SCRIPTS")"
|
currentshebang="$(sed -n '1{s/^#![[:blank:]]*//p;q}' "$SCRIPTS")"
|
||||||
if [ ! -f "${currentshebang%% *}" ]; then
|
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"
|
sed -i "s|$currentshebang|$shebang|g" "$SCRIPTS"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Use source to share env variables when requested
|
# Use source to share env variables when requested
|
||||||
if [ "${ha_entry_source:-null}" = true ] && command -v "source" &>/dev/null; then
|
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/(.*\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.nok/return 1/g" "$SCRIPTS"
|
||||||
sed -i "s/bashio::exit.ok/return 0/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 $?"
|
source "$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?"
|
||||||
else
|
else
|
||||||
# Support for posix only shell
|
|
||||||
"$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?"
|
"$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -58,23 +59,30 @@ if [ "$$" -eq 1 ]; then
|
|||||||
terminate() {
|
terminate() {
|
||||||
echo "Termination signal received, forwarding to subprocesses..."
|
echo "Termination signal received, forwarding to subprocesses..."
|
||||||
|
|
||||||
# Gracefully terminate open 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
|
for pid in /proc/[0-9]*/; do
|
||||||
pid=${pid#/proc/}
|
pid=${pid#/proc/}
|
||||||
pid=${pid%/}
|
pid=${pid%/}
|
||||||
if [[ "$pid" -ne 1 ]]; then
|
if [[ "$pid" -ne 1 ]] && grep -q "^PPid:\s*$$" "/proc/$pid/status" 2>/dev/null; then
|
||||||
echo "Terminating PID $pid"
|
echo "Terminating child PID $pid"
|
||||||
kill -TERM "$pid" 2>/dev/null || echo "Failed to terminate PID $pid"
|
kill -TERM "$pid" 2>/dev/null || echo "Failed to terminate PID $pid"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
# Wait for all child processes to terminate
|
|
||||||
wait
|
wait
|
||||||
echo "All subprocesses terminated. Exiting."
|
echo "All subprocesses terminated. Exiting."
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
trap terminate SIGTERM SIGINT
|
trap terminate SIGTERM SIGINT
|
||||||
while true; do sleep 86400 & wait $!; done
|
while :; do sleep infinity & wait $!; done
|
||||||
else
|
else
|
||||||
echo " "
|
echo " "
|
||||||
echo -e "\033[0;32mStarting the upstream container\033[0m"
|
echo -e "\033[0;32mStarting the upstream container\033[0m"
|
||||||
|
|||||||
Reference in New Issue
Block a user