#!/bin/bash # Daily restart of project systemd services (local only). # Run via cron. Requires passwordless sudo for systemctl restart. # Log: data/restart-services.log # # Local only: no SSH, no remote commands. Manages systemd units and # local Docker (mempool stack, bitcoind container) on the current machine. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_DIR="$(dirname "$SCRIPT_DIR")" LOG_FILE="$SCRIPT_DIR/restart-services.log" MEMPOOL_COMPOSE="${PROJECT_DIR}/mempool/docker-compose.signet.yml" BITCOIND_CONTAINER="bitcoin-signet-instance" SERVICES=( bitcoin-signet bitcoin anchorage-api api-relay clamav-api faucet-api filigrane-api signet-dashboard userwallet website-skeleton ) log() { echo "$(date -Iseconds) $*" | tee -a "$LOG_FILE"; } cd "$PROJECT_DIR" || exit 1 log "=== Restart services cron (local only, no SSH) ===" # 1. Verify enabled (warn if not) for u in "${SERVICES[@]}"; do if ! systemctl is-enabled "$u" &>/dev/null; then log "WARN: $u is not enabled (will not start at boot)" fi done # 2. Restart active units; track attempted restarted=() for u in "${SERVICES[@]}"; do if ! systemctl is-active --quiet "$u" 2>/dev/null; then log "SKIP: $u not active (not installed or not running on this machine)" continue fi log "Restarting $u..." if sudo systemctl restart "$u"; then log " $u OK" restarted+=("$u") else log " $u FAILED" restarted+=("$u") fi done # 3. Verify active for restarted units for u in "${restarted[@]}"; do if ! systemctl is-active --quiet "$u" 2>/dev/null; then log "WARN: $u not active after restart" fi done # 4. Docker: mempool stack (local only) if [ -f "$MEMPOOL_COMPOSE" ] && command -v docker &>/dev/null; then log "Restarting mempool (docker)..." mem_ok=1 (cd "${PROJECT_DIR}/mempool" && docker compose -f docker-compose.signet.yml restart &>/dev/null) && mem_ok=0 [ "$mem_ok" -ne 0 ] && (cd "${PROJECT_DIR}/mempool" && docker-compose -f docker-compose.signet.yml restart &>/dev/null) && mem_ok=0 if [ "$mem_ok" -eq 0 ]; then log " mempool OK" else log " mempool FAILED" fi else if [ ! -f "$MEMPOOL_COMPOSE" ]; then log "SKIP: mempool compose not found (not deployed on this machine)" else log "SKIP: mempool requires docker (not available)" fi fi # 5. Docker: bitcoind container (local only) if docker ps -a -q -f "name=^${BITCOIND_CONTAINER}$" 2>/dev/null | grep -q .; then log "Restarting $BITCOIND_CONTAINER..." if docker restart "$BITCOIND_CONTAINER" &>/dev/null; then log " $BITCOIND_CONTAINER OK" else log " $BITCOIND_CONTAINER FAILED" fi else log "SKIP: $BITCOIND_CONTAINER container not found (bitcoind may run as systemd)" fi log "=== Done ===" tail -n 100 "$LOG_FILE" > "$LOG_FILE.tmp" && mv "$LOG_FILE.tmp" "$LOG_FILE"