#!/usr/bin/env bash # Restart Besu RPC Node to Clear Mempool # Restarts the Besu service to clear any stuck transactions set -euo pipefail PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}" VMID="${VMID:-2101}" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_success() { echo -e "${GREEN}[✓]${NC} $1"; } log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; } log_error() { echo -e "${RED}[✗]${NC} $1"; } log_section() { echo -e "\n${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"; echo -e "${CYAN}$1${NC}"; echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}\n"; } log_section "Restart Besu RPC Node to Clear Mempool" log_info "VMID: $VMID" log_info "Proxmox Host: $PROXMOX_HOST" # Check if VM is running VM_STATUS=$(ssh root@$PROXMOX_HOST "pct status $VMID 2>&1" || echo "") if ! echo "$VM_STATUS" | grep -q "running"; then log_error "VM $VMID is not running" exit 1 fi log_info "VM is running" # Find Besu process log_section "Finding Besu Process" BESU_PID=$(ssh root@$PROXMOX_HOST "pct exec $VMID -- pgrep -f 'besu\|java.*besu' | head -1" 2>/dev/null || echo "") if [ -n "$BESU_PID" ]; then log_info "Found Besu process: PID $BESU_PID" else log_warn "Besu process not found, checking systemd service..." BESU_SERVICE=$(ssh root@$PROXMOX_HOST "pct exec $VMID -- systemctl list-units --type=service | grep -i besu | awk '{print \$1}' | head -1" 2>/dev/null || echo "") if [ -n "$BESU_SERVICE" ]; then log_info "Found Besu service: $BESU_SERVICE" else log_warn "Besu service not found via systemd" fi fi # Check if Besu is running as Docker container log_section "Checking for Docker Container" BESU_CONTAINER=$(ssh root@$PROXMOX_HOST "pct exec $VMID -- docker ps | grep -i besu | awk '{print \$1}' | head -1" 2>/dev/null || echo "") if [ -n "$BESU_CONTAINER" ]; then log_info "Found Besu Docker container: $BESU_CONTAINER" log_info "Restarting Docker container..." ssh root@$PROXMOX_HOST "pct exec $VMID -- docker restart $BESU_CONTAINER" 2>&1 log_success "Docker container restarted" exit 0 fi # Try systemd restart if [ -n "$BESU_SERVICE" ]; then log_section "Restarting Besu Service" log_info "Restarting $BESU_SERVICE..." ssh root@$PROXMOX_HOST "pct exec $VMID -- systemctl restart $BESU_SERVICE" 2>&1 sleep 5 # Check if service is running SERVICE_STATUS=$(ssh root@$PROXMOX_HOST "pct exec $VMID -- systemctl is-active $BESU_SERVICE" 2>&1 || echo "") if [ "$SERVICE_STATUS" = "active" ]; then log_success "Besu service restarted successfully" else log_error "Besu service restart may have failed: $SERVICE_STATUS" fi exit 0 fi # Try killing and restarting process if [ -n "$BESU_PID" ]; then log_section "Restarting Besu Process" log_warn "Killing Besu process (PID $BESU_PID)..." ssh root@$PROXMOX_HOST "pct exec $VMID -- kill $BESU_PID" 2>&1 sleep 3 # Check if process is still running if ssh root@$PROXMOX_HOST "pct exec $VMID -- pgrep -f 'besu\|java.*besu' >/dev/null 2>&1"; then log_warn "Process still running, using kill -9..." ssh root@$PROXMOX_HOST "pct exec $VMID -- pkill -9 -f 'besu\|java.*besu'" 2>&1 sleep 2 fi log_info "Process killed, waiting for restart..." sleep 10 # Check if process restarted NEW_PID=$(ssh root@$PROXMOX_HOST "pct exec $VMID -- pgrep -f 'besu\|java.*besu' | head -1" 2>/dev/null || echo "") if [ -n "$NEW_PID" ]; then log_success "Besu process restarted (new PID: $NEW_PID)" else log_error "Besu process did not restart automatically" log_info "You may need to manually start Besu" fi exit 0 fi log_error "Could not find Besu process or service to restart" log_info "Please manually restart Besu on VMID $VMID" exit 1