Implement watchdog to server_script.sh

This commit is contained in:
2025-07-04 17:44:10 +02:00
parent ba34ab4167
commit fdc405f853
4 changed files with 43 additions and 11 deletions

View File

@@ -5,6 +5,7 @@ RUN apt-get update && apt-get install -y wget
ENV MAX_LOGS="20"
ENV LOG_DIR="/var/log/glassminers"
ENV LOG_DATE_FORMAT="%Y-%m-%d-%H-%M-%S"
ENV WATCHDOG_ENABLED="false"
RUN mkdir -p ${LOG_DIR}

View File

@@ -9,6 +9,7 @@ services:
- MAX_LOGS=20
# - LOG_DIR="/var/log/glassminers"
# - LOG_DATE_FORMAT="%Y-%m-%d-%H-%M-%S" # No spaces
# - WATCHDOG_ENABLED="false" # Use docker restart behavior instead of internal watchdog
volumes:
- ./logs:/var/log/glassminers
ports:

View File

@@ -8,4 +8,6 @@ if [ $? -ne 0 ]; then
exit 1
fi
chmod 777 ./server.out
echo "[Info] Download completed successfully."

View File

@@ -3,9 +3,10 @@
MAX_LOGS=${MAX_LOGS:-20}
LOG_DIR=${LOG_DIR:-"/var/log/glassminers"}
LOG_DATE_FORMAT=${LOG_DATE_FORMAT:-"%Y-%m-%d-%H-%M-%S"}
WATCHDOG_ENABLED=${WATCHDOG_ENABLED:-false}
MAX_RETRIES=${MAX_RETRIES:-10}
EXEC=./server.out
TIMESTAMP=$(date +"$LOG_DATE_FORMAT")
mkdir -p "$LOG_DIR"
delete_old_logs() {
if [ -d "$LOG_DIR" ]; then
@@ -36,17 +37,44 @@ clean_up() {
delete_old_logs
}
trap 'clean_up; exit 0' SIGTERM
trap 'clean_up; shutdown_requested=true; kill -TERM $SERVER_PID' SIGTERM SIGINT
echo "[Info] Max number of logs set to $MAX_LOGS"
echo "[Info] Server is starting at [$TIMESTAMP]"
$EXEC >"$LOG_DIR/latest.log" &
SERVER_PID=$!
echo "[Info] Watchdog enabled: $WATCHDOG_ENABLED"
echo "[Info] Max retries: $MAX_RETRIES"
wait $SERVER_PID
EXIT_STATUS=$?
shutdown_requested=false
retry_count=0
while true; do
TIMESTAMP=$(date +"$LOG_DATE_FORMAT")
echo "[Info] Server is starting at [$TIMESTAMP]"
$EXEC >"$LOG_DIR/latest.log" 2>&1 &
SERVER_PID=$!
wait $SERVER_PID
EXIT_STATUS=$?
echo "[Info] Server stopped at [$(date +"$LOG_DATE_FORMAT")] with exit code: $EXIT_STATUS"
if [ "$shutdown_requested" = true ] || [ $EXIT_STATUS -eq 0 ]; then
break
fi
if [ "$WATCHDOG_ENABLED" = true ]; then
if [ $retry_count -lt $MAX_RETRIES ]; then
retry_count=$((retry_count + 1))
clean_up
echo "[Warning] Watchdog restarting server after crash (attempt $retry_count/$MAX_RETRIES)"
sleep 5
else
echo "[Error] Max retries reached ($MAX_RETRIES). Watchdog stopping."
break
fi
else
break
fi
done
clean_up
echo "[Info] Server stopped at [$(date +"$LOG_DATE_FORMAT")] with exit code: $EXIT_STATUS"
sleep 5
echo "[Info] Watchdog script exiting"