#!/bin/bash # Start Docker services at boot (bitcoind and mempool). # This script ensures bitcoind starts before mempool. # Can be called from systemd service or cron @reboot. # Log: data/start-docker-services.log # # Local only: no SSH, no remote commands. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_DIR="$(dirname "$SCRIPT_DIR")" LOG_FILE="$SCRIPT_DIR/start-docker-services.log" MEMPOOL_COMPOSE="${PROJECT_DIR}/mempool/docker-compose.signet.yml" BITCOIND_CONTAINER="bitcoin-signet-instance" log() { echo "$(date -Iseconds) $*" | tee -a "$LOG_FILE"; } cd "$PROJECT_DIR" || exit 1 log "=== Start Docker services at boot ===" # Check if docker is available if ! command -v docker &>/dev/null; then log "ERROR: docker command not found" exit 1 fi # 1. Start bitcoind container (must be first) if docker ps -a -q -f "name=^${BITCOIND_CONTAINER}$" 2>/dev/null | grep -q .; then if docker ps -q -f "name=^${BITCOIND_CONTAINER}$" 2>/dev/null | grep -q .; then log "$BITCOIND_CONTAINER already running" else log "Starting $BITCOIND_CONTAINER..." if docker start "$BITCOIND_CONTAINER" &>/dev/null; then log " $BITCOIND_CONTAINER started" # Wait for bitcoind RPC to be ready (max 120 seconds at boot) max_wait=120 wait_count=0 while [ $wait_count -lt $max_wait ]; do if docker exec "$BITCOIND_CONTAINER" bitcoin-cli -datadir=/root/.bitcoin getblockchaininfo &>/dev/null; then log " $BITCOIND_CONTAINER RPC ready after ${wait_count}s" break fi sleep 2 wait_count=$((wait_count + 2)) done if [ $wait_count -ge $max_wait ]; then log " WARN: $BITCOIND_CONTAINER RPC not ready after ${max_wait}s (may still be starting)" fi else log " ERROR: Failed to start $BITCOIND_CONTAINER" fi fi else log "SKIP: $BITCOIND_CONTAINER container not found" fi # 2. Start mempool stack (after bitcoind) if [ -f "$MEMPOOL_COMPOSE" ]; then log "Starting mempool stack..." mem_ok=1 (cd "${PROJECT_DIR}/mempool" && docker compose -f docker-compose.signet.yml up -d &>/dev/null) && mem_ok=0 [ "$mem_ok" -ne 0 ] && (cd "${PROJECT_DIR}/mempool" && docker-compose -f docker-compose.signet.yml up -d &>/dev/null) && mem_ok=0 if [ "$mem_ok" -eq 0 ]; then log " mempool stack started" else log " ERROR: Failed to start mempool stack" fi else log "SKIP: mempool compose not found" fi log "=== Done ===" tail -n 100 "$LOG_FILE" > "$LOG_FILE.tmp" && mv "$LOG_FILE.tmp" "$LOG_FILE"