quicktools / scripts /watchdog.sh
github-actions[bot]
deploy: sync to hugging face
4cb7ab8
#!/bin/bash
# Configuration
LOG_FILE="$HOME/code/visa_photo_maker/watchdog.log"
# Associative array: Port -> PM2 Process Name
declare -A APPS=( ["13002"]="visa-backend" ["13001"]="visa-frontend" )
# Check each port
for PORT in "${!APPS[@]}"; do
APP_NAME=${APPS[$PORT]}
# Check if port is listening (local only)
if ! nc -z localhost $PORT; then
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
echo "[$TIMESTAMP] ๐Ÿšจ ALERT: $APP_NAME (Port $PORT) is DOWN!" >> $LOG_FILE
# Check if PM2 manages this process before restarting
if pm2 describe $APP_NAME > /dev/null 2>&1; then
echo "[$TIMESTAMP] ๐Ÿ”„ Attempting PM2 restart for $APP_NAME..." >> $LOG_FILE
pm2 restart $APP_NAME
# Double check after 5 seconds
sleep 5
if nc -z localhost $PORT; then
echo "[$TIMESTAMP] โœ… RECOVERY: $APP_NAME is back online." >> $LOG_FILE
else
echo "[$TIMESTAMP] โŒ ERROR: $APP_NAME failed to restart." >> $LOG_FILE
fi
else
echo "[$TIMESTAMP] โš ๏ธ WARNING: PM2 process '$APP_NAME' not found!" >> $LOG_FILE
fi
fi
done