-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_and_restart_container.sh
40 lines (29 loc) · 1.26 KB
/
check_and_restart_container.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash
# Define the container name
CONTAINER_NAME="CONTAINER-NAME-HERE" # -> "big-bear-openvpn-as" For the OpenVPN Container
sleep 120
# Check if the container is running
if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
# Get the container restart count
RESTART_COUNT=$(docker inspect --format='{{.RestartCount}}' $CONTAINER_NAME 2>/dev/null)
# Check if the restart count is greater than 0
if [ "$RESTART_COUNT" -gt 0 ]; then
sleep 60
fi
# Get the container restart count
RESTART_COUNT=$(docker inspect --format='{{.RestartCount}}' $CONTAINER_NAME 2>/dev/null)
# Check if the restart count is greater than 0
if [ "$RESTART_COUNT" -gt 0 ]; then
echo "Container $CONTAINER_NAME is restarting. Restarting it now..."
# Change directory and restart the container
cd /var/lib/casaos/apps/$CONTAINER_NAME || { echo "Failed to change directory"; exit 1; }
docker compose stop $CONTAINER_NAME
docker compose rm -f $CONTAINER_NAME
docker compose up -d $CONTAINER_NAME
echo "Container $CONTAINER_NAME has been restarted."
else
echo "Container $CONTAINER_NAME is running normally."
fi
else
echo "Container $CONTAINER_NAME is not running."
fi