#!/bin/bash # Setup and run script for the remote server set -euo pipefail if [ $# -lt 2 ]; then echo "Usage: $0 " exit 1 fi NAME="$1" PORT="$2" # Validate port range if [ "$PORT" -lt 8001 ] || [ "$PORT" -gt 8003 ]; then echo "Error: PORT must be between 8001 and 8003 (inclusive)" exit 1 fi cd ~/hackathon/"$PORT" # Ensure ~/.local/bin is in PATH export PATH="$HOME/.local/bin:$PATH" # Check if uv is installed, if not install it if ! command -v uv &> /dev/null; then echo "Installing uv..." curl -Ls https://astral.sh/uv/install.sh | sh fi # Check if make is installed, if not install it if ! command -v make &> /dev/null; then echo "Installing make..." sudo apt install make -y fi # Kill any process using the specified port echo "Unregistering and killing bod running on port $PORT..." fuser -k -TERM "${PORT}/tcp" >/dev/null 2>&1 || true # Wait until all processes on the port are terminated echo "Waiting for port $PORT to be released..." MAX_RETRIES=30 RETRY_COUNT=0 while lsof -Pi :"${PORT}" -sTCP:LISTEN | grep -q LISTEN && [ $RETRY_COUNT -lt $MAX_RETRIES ]; do sleep 2 RETRY_COUNT=$((RETRY_COUNT + 1)) done if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then echo "Error: Port $PORT is still in use after ${MAX_RETRIES} attempts" exit 1 fi # Run the code in the background so script can exit echo "Starting bot $NAME on port $PORT..." nohup make run PORT="$PORT" IDENTITY_KEY="$NAME" > server.log 2>&1 & echo "Bot started!"