Skip to content

AsiaOstrich/claude-code-auto-updater

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Claude Code Auto-Updater

跨平台的 Claude Code 自動更新 + 持續在線方案。讓 Claude Code 保持常駐運行,並每日自動檢查更新。

功能

  • 持續在線 — Claude Code process 異常退出後自動重啟
  • 自動更新 — 每日定時檢查 @anthropic-ai/claude-code 新版本,有更新則自動安裝並重啟
  • 跨平台 — 支援 macOS (launchd)、Linux (systemd)、Windows (Task Scheduler)
  • 可配置 — Channel plugin、工作目錄、更新時間等皆可自訂
  • 版本鎖定 — 支援鎖定特定版本 (PIN_VERSION) 或設定版本上限 (MAX_VERSION)
  • Dry-run--dry-run 只檢查有無新版,不實際安裝
  • 狀態面板status.sh / status.ps1 一鍵查看服務狀態和更新歷史
  • Log 輪替 — 自動清理超過保留天數的 log
  • 更新 Hook — 更新前後可執行自訂腳本

前置需求

快速開始

1. 複製專案

git clone https://github.com/AsiaOstrich/claude-code-auto-updater.git
cd claude-code-auto-updater

2. 建立設定檔

cp config.env.example config.env

編輯 config.env,填入你的設定:

# 要執行的 Claude Code 完整指令
CLAUDE_COMMAND="claude --channels plugin:telegram@claude-plugins-official"

# Claude Code 的工作目錄
CLAUDE_WORKING_DIR="$HOME/claude-workspace"

# 每日更新檢查時間(24 小時制,0-23)
UPDATE_HOUR=4

# Log 保留天數
LOG_RETENTION_DAYS=30

# Service 名稱(用於系統服務識別)
SERVICE_NAME="claude-code-always-on"

3. 執行平台對應的 setup 腳本

Linux (systemd)

chmod +x linux/setup.sh
./linux/setup.sh

macOS (launchd)

chmod +x macos/setup.sh
./macos/setup.sh

Windows (Task Scheduler)

以 PowerShell 執行:

powershell -ExecutionPolicy Bypass -File .\windows\setup.ps1

架構

元件 A:常駐服務(保活)
├── Linux:   systemd user service (Restart=always)
├── macOS:   launchd agent (KeepAlive=true)
└── Windows: Scheduled Task + watchdog.ps1 (迴圈重啟)

元件 B:定時更新
├── Linux:   systemd timer → update-claude.sh
├── macOS:   launchd StartCalendarInterval → update-claude.sh
└── Windows: Scheduled Task → update-claude.ps1

更新流程

定時觸發 → npm view 查最新版 → 比對本機版本
  → 相同:log "無需更新" → 結束
  → 不同:npm install → 重啟服務 → log "已更新" ✓
  → 超過 MAX_VERSION:跳過 → log "超過上限" ✓

進階設定

版本鎖定

# 鎖定特定版本(優先級最高)
PIN_VERSION="1.0.20"

# 設定版本上限(PIN_VERSION 未設定時生效)
MAX_VERSION="2.0.0"

更新 Hook

# 更新前後執行自訂腳本
PRE_UPDATE_HOOK="/path/to/pre-update.sh"
POST_UPDATE_HOOK="/path/to/post-update.sh"

Dry-run 模式

只檢查有無新版,不實際安裝:

# macOS / Linux
./scripts/update-claude.sh --dry-run

# Windows
powershell -File .\scripts\update-claude.ps1 -DryRun

查看狀態

# macOS / Linux
./scripts/status.sh

# 顯示最近 10 筆更新記錄
./scripts/status.sh --log 10

# Windows
powershell -File .\scripts\status.ps1
powershell -File .\scripts\status.ps1 -Log 10

輸出範例:

╔══════════════════════════════════════╗
║   Claude Code Auto-Updater Status   ║
╠══════════════════════════════════════╣
║  Service:  Running (PID: 12345)     ║
║  Version:  1.0.23                   ║
║  Uptime:   3d 14h 22m              ║
║  Platform: Linux (systemd)          ║
╠══════════════════════════════════════╣
║  Last Update Check                  ║
║  [2026-03-30 04:00:03] No update    ║
║  needed: 1.0.23 is current         ║
╠══════════════════════════════════════╣
║  Config                             ║
║  Command:  claude --channels ...    ║
║  WorkDir:  /home/user/workspace     ║
║  Schedule: Daily at 04:00           ║
╚══════════════════════════════════════╝

目錄結構

claude-code-auto-updater/
├── config.env.example          # 設定檔範本
├── scripts/
│   ├── update-claude.sh        # macOS + Linux 更新腳本
│   ├── update-claude.ps1       # Windows 更新腳本
│   ├── status.sh               # macOS + Linux 狀態面板
│   └── status.ps1              # Windows 狀態面板
├── linux/
│   ├── setup.sh                # 安裝 systemd 服務
│   └── uninstall.sh            # 移除 systemd 服務
├── macos/
│   ├── setup.sh                # 安裝 launchd agent
│   └── uninstall.sh            # 移除 launchd agent
└── windows/
    ├── setup.ps1               # 安裝排程任務
    ├── uninstall.ps1           # 移除排程任務
    └── watchdog.ps1            # 保活迴圈腳本

Log 位置

平台 服務 Log 更新 Log
Linux journalctl --user -u $SERVICE_NAME ~/.local/share/claude-code/logs/updater.log
macOS ~/Library/Logs/claude-code/service.log ~/Library/Logs/claude-code/updater.log
Windows %LOCALAPPDATA%\claude-code\logs\service.log %LOCALAPPDATA%\claude-code\logs\updater.log

解除安裝

Linux

./linux/uninstall.sh

macOS

./macos/uninstall.sh

Windows

powershell -ExecutionPolicy Bypass -File .\windows\uninstall.ps1

注意事項

  • 重啟後是全新 Claude Code session,之前的對話 context 會遺失,但 Claude Code 記憶系統的內容會保留
  • Linux 需要啟用 user lingering(loginctl enable-linger),setup 腳本會自動處理
  • macOS launchd 有預設 throttle 機制,快速失敗時重啟間隔可能達 5-10 秒
  • config.env 包含你的個人設定,已加入 .gitignore,不會被提交
  • 修改 config.env 後重新執行 setup 腳本即可更新設定(冪等操作)

License

MIT

About

Systemd-based auto-updater and daemon manager for Claude Code Channels. Keeps your always-on Claude Code instance up-to-date with zero downtime.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors