Skip to content

Commit

Permalink
reworked restart behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Wargamer-Senpai committed Dec 6, 2023
1 parent 723fcaf commit b3b91a4
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions watchdog.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
#!/bin/bash
if [[ $CONTAINER_BOOL == "True" ]]; then
COUNT_RAM_FAILS=0
KILL_CONTAINER=false
#
#
# this is a script to watch over the bot, the bot for some reason crashes/freezes inside a container.
#
BOT_PID=0

/usr/bin/python3 /opt/teampy/main.py &
STARTUP_CODE=$?
if [[ $STARTUP_CODE -ne 0 ]]; then
echo "[ERROR] Startup failed (Code $STARTUP_CODE)"
exit 5

# function kill_container(){
# exit 69
# }

function start_bot(){
if [[ $BOT_PID -ne 0 ]]; then
kill -9 $BOT_PID
fi

/usr/bin/python3 /opt/teampy/main.py &
BOT_PID=$!
/bin/sleep 60
}


if [[ $CONTAINER_BOOL == "True" ]]; then
COUNT_RAM_FAILS=0
echo "Started Container $(date)"
start_bot

# CHECK RAM
while true; do
# check change timestamp
TIMESTAMP_FILE=$(stat check_container | grep Change | awk -F: '{print $4}' | awk -F. '{print $1}')
TIMESTAMP_DATE=$(date | awk -F: '{print $3}' | awk '{print $1}')
TIMESTAMP_DIFF=$((10#$TIMESTAMP_DATE-10#$TIMESTAMP_FILE))

# if time difrence is greater then 10 seconds kill, #0 casts the number to be an INT not Octal
if [[ ${TIMESTAMP_DIFF#0} -gt 10 ]]; then
echo '[ERROR] Bot didnt touched file, killing the bot *bonk*'
start_bot
fi

# get ram usage
# shellcheck disable=SC2062
RAM_USAGE=$(top -n 1 | grep python[3] | awk '{print $8}')
Expand All @@ -22,16 +49,14 @@ if [[ $CONTAINER_BOOL == "True" ]]; then
else
COUNT_RAM_FAILS=0
fi

if [[ $COUNT_RAM_FAILS -eq 50 ]]; then
KILL_CONTAINER=true
echo '[ERROR] Bot didnt used ram for a long time, killing the bot *bonk*'
start_bot
fi
if [[ "$RAM_USAGE" == "" || "$RAM_USAGE" == " " || -z "$RAM_USAGE" ]]; then
KILL_CONTAINER=true
fi

# kill the bot if it gues its to much
if $KILL_CONTAINER; then
exit 69
echo '[ERROR] Where is the PID, killing the bot *bonk*'
start_bot
fi

/bin/sleep 1
Expand Down

0 comments on commit b3b91a4

Please sign in to comment.