如何将 AI Agent 操作从 VPS 迁移到 Mac Mini(不破坏 43 个 Cron Jobs)

发布: (2026年3月1日 GMT+8 01:12)
5 分钟阅读
原文: Dev.to

Source: Dev.to

(请提供您希望翻译的正文内容,我将按照要求保留链接并翻译文本。)

概览

将 OpenClaw AI Agent 从 VPS 迁移到 Mac Mini(macOS 14.6.0),并保持所有 43 个 cron 任务完整。到第 4 天,系统实现了 70 % 的成功率,且零人工干预。关键成功因素是 LaunchAgent 的自动启动配置以及 API 身份验证令牌的迁移。

准备

  • 环境: 在 VPS 上的 OpenClaw AI Agent 执行平台。
  • 目标: 带 SSH 访问的 Mac Mini(macOS 14.6.0)。
  • 凭证: GitHub、Anthropic 等的 API 令牌。
  • 参考: Site24x7 – Server Migration Best Practices – “通过映射所有依赖来规划迁移策略”。

映射现有的 Cron 任务

# Extract current cron schedule
crontab -l > current-crons.txt

# Analyse 43 jobs by priority
grep -E "article-writer|trend-hunter|x-poster" current-crons.txt
优先级任务类型计数备注
关键daily‑memory, article‑writer5每日记录系统
x‑poster, trend‑hunter15内容发布
中等autonomy‑check, app‑metrics23监控与指标

自动启动的 LaunchAgent

创建 LaunchAgent plist(保存为 ~/Library/LaunchAgents/com.openclaw.gateway.plist):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.openclaw.gateway</string>
    <key>Program</key>
    <string>/opt/homebrew/bin/openclaw</string>
    <key>ProgramArguments</key>
    <array>
        <string>/opt/homebrew/bin/openclaw</string>
        <string>gateway</string>
        <string>start</string>
    </array>
    <key>WorkingDirectory</key>
    <string>/Users/anicda/.openclaw</string>
    <key>KeepAlive</key>
    <true/>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

加载并启动代理:

launchctl load ~/Library/LaunchAgents/com.openclaw.gateway.plist
launchctl start com.openclaw.gateway

注意: LaunchAgent 是 macOS 上相当于 systemd 的机制;KeepAlive=true 可确保自动恢复。

Source: OpenClaw Issue #21508 – SSH Keychain access problems.

Claude Code 认证(SSH 会话)

# Set the OAuth token for Claude Code
export CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-xxx...
echo 'export CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-xxx...' >> ~/.zshrc

API 密钥设置

~/.openclaw/.env 中存储环境变量:

# X/TikTok posting
BLOTATO_API_KEY=xxx
BLOTATO_ACCOUNT_ID_EN=xxx
BLOTATO_TIKTOK_ACCOUNT_ID=28152

# Article publishing
GITHUB_TOKEN=ghp_xxx

# AI APIs
FAL_API_KEY=xxx
ANTHROPIC_API_KEY=xxx

分阶段迁移(而非一次性大迁移)

步骤 4.1 – 首先迁移关键优先级

0 6 * * * cd /Users/anicca/.openclaw && openclaw execute daily-memory

步骤 4.2 – 确认后添加高优先级

0 9 * * * cd /Users/anicca/.openclaw && openclaw execute x-poster
0 21 * * * cd /Users/anicca/.openclaw && openclaw execute x-poster-evening

最佳实践: 先从关键 → 高 → 中,以便将任何问题局部化。

常见故障排除

问题症状解决方案
PATH 未设置command not foundexport PATH=/opt/homebrew/bin:$PATH 添加到 cron 环境中。
工作目录“没有此文件”在命令前加上 cd /Users/anicca/.openclaw && 前缀。
API 认证失败401 Unauthorized重新加载 .env 文件或验证令牌值。

通过 Slack 的集中监控

# Mandatory Slack reporting after each skill execution
openclaw message send --channel slack --target 'C091G3PKHL2' \
  --message "✅ ${SKILL_NAME} completed"

成功率测量

# Count success/failure over the past 7 days
grep -E "(completed|error)" /var/log/system.log |
  grep "$(date -v-7d +%Y-%m-%d)" |
  wc -l

“Karuna (Compassion)‑Based” 内容策略

# content-strategy.py
def generate_mindful_content():
    # Buddhist principles: Solutions > Product promotion
    approach = {
        "ehipassiko": "Come, see, verify for yourself",
        "karuna": "Lead with empathy and compassion",
        "no_pushy_sales": "Complete avoidance of aggressive sales"
    }
    return generate_content(approach)

关键经验

  • LaunchAgent 对于 macOS 上的自动恢复至关重要。
  • 将身份验证变量保留在 SSH 密钥链之外,以避免访问问题。
  • 分阶段迁移可降低风险;先处理关键作业。
  • 集中式监控(例如 Slack)提供统一的执行报告。
  • 70% 的成功率且无需人工干预,是自主代理的现实且可接受的目标。

结果

在迁移后第 4 天不需要任何人工干预。对于 AI 代理的自主操作而言,“从故障中自动恢复”比实现 100 % 完美更为重要。

0 浏览
Back to Blog

相关文章

阅读更多 »

当工作成为心理健康风险时

markdown !Ravi Mishrahttps://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fu...