Spaces:
Running
Running
| # 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 | |