From e96a94bc2d37e3cbe785e9ddb31aa5539d29312f Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sun, 24 Dec 2023 19:19:07 +0100 Subject: [PATCH] Implemented check for excited != 0 https://chat.openai.com/share/981ecca4-9df7-4086-b99e-1b60f2632bf6 --- .../files/health-docker-container.sh | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/roles/health-docker-container/files/health-docker-container.sh b/roles/health-docker-container/files/health-docker-container.sh index 5ae70f89..2b4ae3c7 100644 --- a/roles/health-docker-container/files/health-docker-container.sh +++ b/roles/health-docker-container/files/health-docker-container.sh @@ -1,20 +1,30 @@ #!/bin/sh docker_ps_grep_unhealthy="$(docker ps --filter health=unhealthy --format '{{.Names}}')" -docker_ps_grep_exited="$(docker ps --filter status=exited --format '{{.Names}}')" +docker_ps_grep_exited="$(docker ps --filter status=exited --format '{{.ID}}')" + exitcode=0 -if [ ! -z "$docker_ps_grep_unhealthy" ] -then + +if [ -n "$docker_ps_grep_unhealthy" ]; then echo "Some docker containers are unhealthy: $docker_ps_grep_unhealthy" exitcode=1 fi -if [ ! -z "$docker_ps_grep_exited" ] -then - echo "Some docker containers exited: $docker_ps_grep_exited" - exitcode=2 + +if [ -n "$docker_ps_grep_exited" ]; then + for container_id in $docker_ps_grep_exited + do + container_exit_code="$(docker inspect "$container_id" --format='{{.State.ExitCode}}')" + container_name="$(docker inspect "$container_id" --format='{{.Name}}')" + container_name="${container_name#/}" # Entfernt das führende '/' + if [ "$container_exit_code" -ne "0" ]; then + echo "Container $container_name exited with code $container_exit_code" + exitcode=2 + fi + done fi -if [ "$exitcode" -ne "0" ] -then + +if [ "$exitcode" -ne "0" ]; then exit $exitcode fi + echo "All docker containers are healthy." -exit \ No newline at end of file +exit