基础工具与工作流 · daymade/claude-code-skills

macos-watchdog

Design, deploy, and discipline macOS launchd watchdogs — LaunchAgents/LaunchDaemons that detect a recurring problem and auto-remediate it. Use whenever creating or editing a persistent background monitor / daemon / agent on macOS, writing a launchd plist, scheduling a self-healing script, or when a watchdog has become a disturbance itself: re-launching apps the user quit, firing repeated notifications, re-running its full repair ladder every few minutes on an unfixable network, or hammering the system (crash loops, fork storms, runaway restarts). Also use for stop/disable semantics (bootout vs bootstrap vs disable vs unload), adding cooldown / backoff / notification throttling to a self-healer, binding a monitor's lifecycle to its premise state, or auditing existing LaunchAgents. 中文触发:launchd 守护进程、常驻任务、开机自启、后台监控、定时自愈脚本。 Covers KeepAlive/ThrottleInterval/domains/logging, premise self-checks, auto-cooldown, alert layering, batch throttling.

风险提醒:黄色 · 留意使用AI 侦查报告
作者 daymadeGitHub daymade/claude-code-skills ↗Stars 1385许可 MIT(仓库根 LICENSE,Copyright (c) 2025 daymade;GitHub API spdx MIT)commit d5c4678cb5
agent 宿主通常会约束 skill 执行权限;风险提醒为 AI 侦查观点,不构成质量或安全保证。第三方 skill 仅作拆解与展示,安装使用风险自负,版权归原作者。

1实现原理 · 为什么它能做到

设计哲学:watchdog 的生命周期绑定 premise state——它存在的理由消失(WiFi 断、用户退掉目标 app、前置状态没了)时必须自己 stand down,而不是等人来 disable;本 skill 的核心教导是『别让 watchdog 变成新扰动』。

daymade-macos/macos-watchdog/SKILL.md
**a watchdog's lifecycle is bound to its premise state**. When the condition it exists to fix cannot be fixed by it (broken WiFi, user quit the target app, prerequisite state gone), the watchdog must stand down *by itself*
注:这是全 skill 的主线:部署指南只是外壳,『quiet-watchdog contract』四条款(premise 自检 / 先修后报且 N 轮才升级 / 阶梯自动冷却 / 不复活用户主动退出的 app)才是防扰动的核心。

部署机制走 launchd 原生工具:assets/launchagent.template.plist 作底,new-launchagent.sh 幂等安装(校验 label/interval/program → bootout-if-loaded → 写 plist → plutil -lint → launchctl bootstrap → launchctl print 验证)。

daymade-macos/macos-watchdog/scripts/new-launchagent.sh
plutil -lint "$tmp_plist" >/dev/null || { echo "Generated plist failed plutil -lint" >&2; rm -f "$tmp_plist"; exit 1; }
注:interval <30s 直接拒绝(crash-loop 区);program 必须绝对路径且可执行;label 必须 reverse-DNS;--system 走 sudo 写 /Library/LaunchDaemons + system 域(root daemon 日志不落用户 home)。

冷却逻辑落在应用层而非 ThrottleInterval:watchdog-cooldown.sh 被 source 后提供 paused_any/record_exhausted/clear_exhausted/cmd_pause/cmd_resume,在 state 文件里记录连续 exhausted 轮数并按阶梯(默认 1800/7200/21600 秒 = 30m→2h→6h)退避静默。

daymade-macos/macos-watchdog/scripts/watchdog-cooldown.sh
space-separated escalating tiers, seconds. Default "1800 7200 21600"
注:文档解释为何 ThrottleInterval 不够:它节流的是进程 respawn、是固定延迟无退避、对『exit 0 后照样刷屏』的 job 无效——spam 在应用层,静默也得在应用层。

停止语义把 deprecated 陷阱显式化:unload 在 Ventura+ 会因 plist 还在 + RunAtLoad 复活 job,必须用 bootstrap/bootout(现代对)与 disable/enable(跨登录保持停止)。

daymade-macos/macos-watchdog/SKILL.md
**Never `launchctl unload`**: deprecated, and on Ventura+ the job re-loads via `RunAtLoad` when the plist stays in place — the "disabled" watchdog fires again
注:同一节的命令表给出 bootout(stop now)/disable(stop across login)/bootout→edit→bootstrap(改 plist 后重载)三意图的准确命令。

防 resurrect 门禁:修复若用 URL scheme/open -a/重启 GUI app,必须先用进程存活检查把关并加 -g(不抢前台)——否则 watchdog 会表现为『我退了你又给我开回来』。

daymade-macos/macos-watchdog/SKILL.md
Gate every such action: check the target process is alive before invoking its scheme, and pass `-g` so a legitimate action never pops a window. If the user quit the app mid-remediation, abort the ladder
注:cleanup trap 也要守同一门禁,否则『退出时确保已连接』的兜底会变成复活者。

2核心能力

01新 watchdog 全流程部署(位置选择 LaunchAgents vs LaunchDaemons、plist 模板、加载/重载/日志/幂等)
02quiet-watchdog 四条款契约审查(用于诊断既有 watchdog 的骚扰行为:spam/复活/空转/刷屏)
03幂等安装器 new-launchagent.sh(bootout-if-loaded → 写 plist → bootstrap → launchctl print 验证)
04应用层阶梯冷却/手动暂停(watchdog-cooldown.sh 状态机)
05停止/禁用语义精确表(bootout / disable / enable / unload 陷阱)
06故障排查速查表(复活/刷屏/假健康/bootout 不生效/静默失败/TCC 错解释器)
07批量任务默认限速纪律(防止未节流循环 = 失控进程)

3外部依赖

类型依赖
clilaunchctl(bootstrap/bootout/kickstart/print/list/disable/enable)
cliplutil(plist lint 校验)
clisudo(--system 路径:/Library/LaunchDaemons + system 域 bootstrap)
cliopen / log show / id -u(GUI 域引导、launchd 日志排查)

4风险提醒 风险提醒:黄色 · 留意使用

风险提醒:黄色 · 留意使用
  • 生成并常驻执行代码的能力 — 本 skill 指引模型编写自愈脚本并注册为 launchd job(--system 可为 root 常驻);脚本内容不受 skill 源码约束。注入面=『要修什么』的用户输入与任何污染该描述的内容;使用者应把安装的脚本当代码审查。
  • 系统级持久变更 — LaunchDaemons + sudo 会留下 root 级常驻 job;bootout/disable 语义用错(unload)或 plist 残留会导致 job 复活。删除/禁用需按 stop 语义表精确操作。
  • TCC/Full Disk Access 误配给错误解释器 — LaunchAgent 读受保护目录需 FDA 授予实际解释器(Xcode python3 stub 会失败);文档要求用 ProgramArguments 里的确切二进制验证,否则 watchdog 静默失败或误授权。
  • 冷却状态文件与日志的本地写入 — state 文件默认在 TMPDIR(重启即失,跨重启冷却需指到持久目录);日志无上限会无限增长(模板要求脚本内轮转 cap ~1MB)。
风险提醒:黄色,留意使用。本地 launchd 自动化技能:无网络外发、无凭证读取(模板明示 plist 世界可读勿放密钥);黄色来自其设计目的——把可执行脚本安装为**持久后台 job**(LaunchAgents/LaunchDaemons),--system 路径经 sudo 写 /Library/LaunchDaemons 并 bootstrap 到 system 域,受信脚本将以用户/root 权限按周期常驻执行,属于系统级持久变更面;且模型按本 skill 生成的 watchdog 脚本内容不受 skill 自身约束(仅被四条款纪律与输入校验间接约束)。因无外发/无凭证,不到橙;因是常驻 root 级执行面而非普通本地读写,高于蓝,判黄。

5第二遍独立确认

  • [ok] 四条款契约与真实事故绑定 — SKILL.md 每条 clause 后都带真实案例('Real case: a recovery watcher kept firing for 2h after its premise resolved'、'unthrottled test replay forked 1,041 processes/sec for 7 minutes' 等);war stories 在 references/quiet-watchdog-patterns.md。
  • [ok] new-launchagent.sh 校验与安装流程 — label reverse-DNS 校验、interval 整数且 ≥30、program -f/-x 绝对路径、tmp plist + plutil -lint、bootout-if-loaded、launchctl bootstrap + print 验证,全部逐字在源码。
  • [ok] cool-down 状态机(1800/7200/21600 阶梯) — watchdog-cooldown.sh 常量与文档默认一致;state 文件 pause/autopause/exhausted.state;提供 cmd_pause/cmd_resume/record_exhausted/clear_exhausted/paused_any。
  • [ok] unload 陷阱与 bootstrap/bootout 语义 — SKILL.md 'Never `launchctl unload`' + Ventura+ RunAtLoad 复活机制说明 + bootout/disable 命令表,与脚本实现一致。
  • [ok] 运行路径无网络(安全结论基础) — 全目录运行代码无 curl/wget/http 调用;唯一 curl 出现在 evals/iteration-1 fixture broken-heal.sh(评测反例夹具),已在 internal_assets/风险注记中说明非运行路径。
  • [ok] 无凭证读取 + plist 密钥警告 — plist 模板 'Never put secrets here; the plist is world-readable.';脚本无 token/API key 处理。
  • [ok] 元数据(commit/license/stars) — 本地 HEAD == pin d5c4678cb5d4fd6acc9c922690df035dbd33d247;GitHub API:MIT、stars 1385(2026-09-09 实采)、pushed_at 2026-09-09T12:33:29Z。

6结论

  • 真正理解『watchdog 会变成新故障』:把自愈者的失序(复活/刷屏/空转/fork 风暴)当成一等问题处理。
  • 应用层冷却与 premise 自检是可落地的工程模式:ThrottleInterval 解决不了应用层 spam,state 文件 + 阶梯退避才是。
  • 事故驱动、教训带血:每条纪律附真实案例与观测数据(N=2 cycles 来自 94-min 观测),不是纸上谈兵。
  • 幂等与验证内建:new-launchagent.sh 收敛重跑、plutil -lint、bootstrap 后 print 验证,部署不会越跑越乱。
  • 防 resurrect 与批量限速这类『副作用纪律』被写成显式门禁与默认参数,堵住最常见的两类 watchdog 公害。
  • 适合:适合 macOS 上需要『周期性检测 + 自动修复』的常驻监控场景:网络/代理/配置回滚/服务保活类 watchdog 的新建与治理,以及现有 watchdog 变成骚扰源(复活 app、刷通知、空转锤系统)时的诊断修复;适合能自己写/审 watchdog 脚本、理解 launchd 域模型的工程型用户。
    不适合:不适合一次性修复(不需要常驻)的场景;不适合没有脚本编写/审查能力却要部署 root daemon 的用户;不适合非 macOS 平台;不适合期望『装完就不用管』的人——本 skill 的核心恰恰是 watch the watchdog。
    安装 agent 直装可复制
    ① 本站镜像 更新 2026-09-09
    方式 A · 人下载镜像包下载 macos-watchdog.tar.gz
    sha256: 84c303ee98ee57c6…
    方式 B · JSON 格式安装指南,复制给 agent
    安装指南
    agent 读 JSON 指南后会自动从本站下载安装,无需更多说明。
    ② 上游 GitHub · 原始来源
    能访问 GitHub?直接去上游安装(实时版,可能已更新)GitHub 原始 ↗
    本页镜像锁定 commit d5c4678cb5;上游为实时仓库。
    来源信息 GitHub 原始
    作者 / 仓库daymade / daymade/claude-code-skills
    Stars1385
    最近推送2026-09-09
    本 skill commitd5c4678cb5
    许可MIT(仓库根 LICENSE,Copyright (c) 2025 daymade;GitHub API spdx MIT)
    本站信息
    收录日期2026-09-06
    分类基础工具与工作流
    侦查报告AI 侦查 · 2 遍 · 2026-09-06
    本站镜像与 GitHub 原始是不同来源:本站锁定 commit 快照经 /r2 分发;GitHub 为实时上游,内容可能已更新。
    同分类邻近