#!/bin/bash # # notebooklm_auth_watchdog.sh - Check NotebookLM auth and revive if needed. # Runs via cronjob, no-agent mode. Delivers output only on error or change. # CHROME_PORT=9223 PROFILE="default" NLM_CLI_PATH="/opt/ai-os/products/ceo/integrations/notebooklm-mcp-cli/.venv/bin/nlm" REFRESH_SCRIPT="/opt/ai-os/products/ceo/scripts/notebooklm_refresh.py" REVIVE_SCRIPT="/opt/ai-os/products/ceo/scripts/revive_notebooklm_auth.sh" LOG_FILE="/root/.hermes/cron/output/notebooklm-auth.log" # --- Function to log messages --- log_msg() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" } # --- Main execution --- # Check current auth status using NLM CLI's check_auth AUTH_STATUS=$($NLM_CLI_PATH login --check --profile "$PROFILE" 2>&1) NLM_AUTH_CHECK_EXIT_CODE=$? # Check if Chrome headless is alive and responsive CHROME_CHECK=$(curl -s "http://127.0.0.1:$CHROME_PORT/json/version" 2>/dev/null) CHROME_ALIVE_EXIT_CODE=$? if [ "$CHROME_ALIVE_EXIT_CODE" -ne 0 ] || [ -z "$CHROME_CHECK" ]; then log_msg "CRITICAL: Chrome headless on port $CHROME_PORT is DEAD." echo "CRITICAL: Chrome headless on port $CHROME_PORT is DEAD. Attempting revive..." if "$REVIVE_SCRIPT" --port "$CHROME_PORT" --profile "$PROFILE"; then log_msg "Revive script successful. Auth likely restored." echo "Chrome revived successfully. Auth likely restored." exit 0 else log_msg "Revive script FAILED. Manual intervention needed." echo "Revive script failed. Manual intervention needed. Check logs: $LOG_FILE" exit 1 fi elif [ "$NLM_AUTH_CHECK_EXIT_CODE" -ne 0 ] || [[ ! "$AUTH_STATUS" =~ "Authentication valid" ]]; then log_msg "WARNING: NotebookLM auth check failed or reported invalid ($AUTH_STATUS)." echo "WARNING: NotebookLM auth check failed or reported invalid ($AUTH_STATUS). Attempting refresh..." if "$REFRESH_SCRIPT" --port "$CHROME_PORT" --profile "$PROFILE"; then log_msg "Refresh script successful. Auth restored." echo "Refresh script successful. Auth restored." exit 0 else log_msg "Refresh script FAILED. Manual intervention needed." echo "Refresh script failed. Manual intervention needed. Check logs: $LOG_FILE" exit 1 fi else # Auth is valid and Chrome is alive, nothing to do log_msg "INFO: NotebookLM auth check passed and Chrome is alive. No action needed." exit 0 fi