From f6ff0e97ac7127f861e614f0fc0aeaaaaa6207b6 Mon Sep 17 00:00:00 2001 From: DyonR Date: Sun, 20 Feb 2022 02:02:20 +0100 Subject: [PATCH] Added RESTART_CONTAINER If RESTART_CONTAINER is **not** set to `1`, `true` or `yes`, it will **not restart** automatically `exit 1` the container. --- README.md | 1 + qbittorrent/start.sh | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 972f951..5bcea26 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ $ docker run -d \ |`HEALTH_CHECK_HOST`| No |This is the host or IP that the healthcheck script will use to check an active connection|`HEALTH_CHECK_HOST=one.one.one.one`|`one.one.one.one`| |`HEALTH_CHECK_INTERVAL`| No |This is the time in seconds that the container waits to see if the internet connection still works (check if VPN died)|`HEALTH_CHECK_INTERVAL=300`|`300`| |`HEALTH_CHECK_SILENT`| No |Set to `1` to supress the 'Network is up' message. Defaults to `1` if unset.|`HEALTH_CHECK_SILENT=1`|`1`| +|`RESTART_CONTAINER`| No |Set to `no` to **disable** the automatic restart when the network is possibly down.|`RESTART_CONTAINER=yes`|`yes`| |`INSTALL_PYTHON3`| No |Set this to `yes` to let the container install Python3.|`INSTALL_PYTHON3=yes`|`no`| |`ADDITIONAL_PORTS`| No |Adding a comma delimited list of ports will allow these ports via the iptables script.|`ADDITIONAL_PORTS=1234,8112`|| diff --git a/qbittorrent/start.sh b/qbittorrent/start.sh index c13f935..c0eb1ae 100644 --- a/qbittorrent/start.sh +++ b/qbittorrent/start.sh @@ -109,7 +109,7 @@ qbittorrentpid=$(cat /var/run/qbittorrent.pid) # If the process exists, make sure that the log file has the proper rights and start the health check if [ -e /proc/$qbittorrentpid ]; then echo "[INFO] qBittorrent PID: $qbittorrentpid" | ts '%Y-%m-%d %H:%M:%.S' - + # trap the TERM signal for propagation and graceful shutdowns handle_term() { echo "[INFO] Received SIGTERM, stopping..." | ts '%Y-%m-%d %H:%M:%.S' @@ -120,13 +120,13 @@ if [ -e /proc/$qbittorrentpid ]; then if [[ -e /config/qBittorrent/data/logs/qbittorrent.log ]]; then chmod 775 /config/qBittorrent/data/logs/qbittorrent.log fi - + # Set some variables that are used HOST=${HEALTH_CHECK_HOST} DEFAULT_HOST="one.one.one.one" INTERVAL=${HEALTH_CHECK_INTERVAL} DEFAULT_INTERVAL=300 - + # If host is zero (not set) default it to the DEFAULT_HOST variable if [[ -z "${HOST}" ]]; then echo "[INFO] HEALTH_CHECK_HOST is not set. For now using default host ${DEFAULT_HOST}" | ts '%Y-%m-%d %H:%M:%.S' @@ -138,22 +138,33 @@ if [ -e /proc/$qbittorrentpid ]; then echo "[INFO] HEALTH_CHECK_INTERVAL is not set. For now using default interval of ${DEFAULT_INTERVAL}" | ts '%Y-%m-%d %H:%M:%.S' INTERVAL=${DEFAULT_INTERVAL} fi - + # If HEALTH_CHECK_SILENT is zero (not set) default it to supression if [[ -z "${HEALTH_CHECK_SILENT}" ]]; then echo "[INFO] HEALTH_CHECK_SILENT is not set. Because this variable is not set, it will be supressed by default" | ts '%Y-%m-%d %H:%M:%.S' HEALTH_CHECK_SILENT=1 fi + if [ ! -z ${RESTART_CONTAINER} ]; then + echo "[INFO] RESTART_CONTAINER defined as '${RESTART_CONTAINER}'" | ts '%Y-%m-%d %H:%M:%.S' + else + echo "[WARNING] RESTART_CONTAINER not defined,(via -e RESTART_CONTAINER), defaulting to 'yes'" | ts '%Y-%m-%d %H:%M:%.S' + export RESTART_CONTAINER="yes" + fi + while true; do # Ping uses both exit codes 1 and 2. Exit code 2 cannot be used for docker health checks, therefore we use this script to catch error code 2 ping -c 1 $HOST > /dev/null 2>&1 STATUS=$? if [[ "${STATUS}" -ne 0 ]]; then - echo "[ERROR] Network is down, exiting this Docker" | ts '%Y-%m-%d %H:%M:%.S' - exit 1 + echo "[ERROR] Network is possibly down." | ts '%Y-%m-%d %H:%M:%.S' + sleep 1 + if [[ ${RESTART_CONTAINER,,} == "1" || ${RESTART_CONTAINER,,} == "true" || ${RESTART_CONTAINER,,} == "yes" ]]; then + echo "[INFO] Restarting container." | ts '%Y-%m-%d %H:%M:%.S' + exit 1 + fi fi - if [ "${HEALTH_CHECK_SILENT}" == "0" ] || [ "${HEALTH_CHECK_SILENT}" == "false" ] || [ "${HEALTH_CHECK_SILENT}" == "no" ]; then + if [[ ${HEALTH_CHECK_SILENT,,} == "0" || ${HEALTH_CHECK_SILENT,,} == "false" || ${HEALTH_CHECK_SILENT,,} == "no" ]]; then echo "[INFO] Network is up" | ts '%Y-%m-%d %H:%M:%.S' fi sleep ${INTERVAL} &