#!/bin/bash # C-Relay Build and Restart Script # Builds the project first, then stops any running relay and starts a new one in the background echo "=== C Nostr Relay Build and Restart Script ===" # Build the project first echo "Building project..." make clean all # Check if build was successful if [ $? -ne 0 ]; then echo "ERROR: Build failed. Cannot restart relay." exit 1 fi # Check if relay binary exists after build if [ ! -f "./src/main" ]; then echo "ERROR: Relay binary not found after build. Build may have failed." exit 1 fi echo "Build successful. Proceeding with relay restart..." # Kill existing relay if running echo "Stopping any existing relay servers..." pkill -f "./src/main" 2>/dev/null sleep 2 # Give time for shutdown # Check if port is still bound if lsof -i :8888 >/dev/null 2>&1; then echo "Port 8888 still in use, force killing..." fuser -k 8888/tcp 2>/dev/null || echo "No process on port 8888" fi # Get any remaining processes REMAINING_PIDS=$(pgrep -f "./src/main" || echo "") if [ -n "$REMAINING_PIDS" ]; then echo "Force killing remaining processes: $REMAINING_PIDS" kill -9 $REMAINING_PIDS 2>/dev/null sleep 1 else echo "No existing relay found" fi # Clean up PID file rm -f relay.pid # Initialize database if needed if [ ! -f "./db/c_nostr_relay.db" ]; then echo "Initializing database..." ./db/init.sh --force >/dev/null 2>&1 fi # Start relay in background with output redirection echo "Starting relay server..." echo "Debug: Current processes: $(ps aux | grep './src/main' | grep -v grep || echo 'None')" # Start relay in background and capture its PID ./src/main > relay.log 2>&1 & RELAY_PID=$! echo "Started with PID: $RELAY_PID" # Check if server is still running after short delay sleep 3 # Check if process is still alive if ps -p "$RELAY_PID" >/dev/null 2>&1; then echo "Relay started successfully!" echo "PID: $RELAY_PID" echo "WebSocket endpoint: ws://127.0.0.1:8888" echo "Log file: relay.log" echo "" # Save PID for debugging echo $RELAY_PID > relay.pid echo "=== Relay server running in background ===" echo "To kill relay: pkill -f './src/main'" echo "To check status: ps aux | grep src/main" echo "To view logs: tail -f relay.log" echo "Ready for Nostr client connections!" else echo "ERROR: Relay failed to start" echo "Debug: Check relay.log for error details:" echo "--- Last 10 lines of relay.log ---" tail -n 10 relay.log 2>/dev/null || echo "No log file found" echo "--- End log ---" exit 1 fi echo ""