This repository is a public support entry for AI development workstation hygiene. It provides a conservative cross-platform CLI for scanning and cleaning orphan opencode processes, plus watch-mode memory warnings and macOS/Linux service installation paths.
For interviews, use it as a compact example of operational reliability work around AI coding tools: process classification, safe defaults, CLI ergonomics, background service integration, and low-risk automation design.
cmd/oc-cleaner: CLI entry point.internal: process scanning, cleaning, watch, and service logic.- README sections
scan,clean,watch, andservice install: public usage surface.
This repository should not include private process dumps, user session contents, prompts, tokens, machine hostnames, internal automation credentials, or production logs. Keep examples generic and preserve the conservative default behavior: scan first, terminate only narrow orphan-process candidates, and avoid interrupting active sessions.
跨平台(macOS / Linux)自动清理 opencode 孤儿进程的小工具。
默认使用“保守策略”:只处理 PPID=1、无终端绑定、运行时间超过阈值、且无非本地已建立连接的进程。
scan:扫描并输出候选进程clean:温和清理候选(SIGTERM,可选延迟后SIGKILL)watch:内存阈值告警(仅提醒,不重启进程)service install:默认安装并启动两套后台服务(clean + watch)- macOS:
launchd - Linux:
systemd --user timer
- macOS:
service status / uninstall:查看状态或卸载
- 只清理“疑似孤儿且空闲”的
opencode进程,不重启前台正在使用的会话。 - 默认不做“超内存自动重启会话”这类高侵入动作,避免打断正在进行的工作。
- 通过短间隔定时扫描,实现接近实时的后台维护。
git clone https://github.com/l2ktech/12-opencode-cleaner.git
cd 12-opencode-cleaner
go build -o oc-cleaner ./cmd/oc-cleanergo run ./cmd/oc-cleaner scan --min-age-min 10./oc-cleaner scan --min-age-min 10
./oc-cleaner scan --min-age-min 10 --json# 仅 TERM
./oc-cleaner clean --min-age-min 10
# TERM 后 5 秒仍存活则 KILL
./oc-cleaner clean --min-age-min 10 --kill-after-sec 5# 单次检测:超出 1024MB 的进程会输出 WARN
./oc-cleaner watch --warn-mem-mb 1024 --once
# 持续监控:每 30 秒检测一次
./oc-cleaner watch --warn-mem-mb 1024 --interval-sec 30
# JSON 输出(便于接日志系统)
./oc-cleaner watch --warn-mem-mb 1024 --once --json
watch不会发送 TERM/KILL,只做告警输出。
./oc-cleaner service install --interval-sec 120 --min-age-min 10 --watch-interval-sec 30 --watch-warn-mem-mb 1024
./oc-cleaner service status默认会同时启动:
com.wzy.oc-cleaner.clean(后台清理)com.wzy.oc-cleaner.watch(后台监控告警)
卸载:
./oc-cleaner service uninstall./oc-cleaner service install --interval-sec 120 --min-age-min 10 --watch-interval-sec 30 --watch-warn-mem-mb 1024
./oc-cleaner service status默认会同时启动:
oc-cleaner-clean.timer(后台清理)oc-cleaner-watch.timer(后台监控告警)
建议额外开启 lingering(使用户退出登录后仍可运行 user service):
sudo loginctl enable-linger "$USER"
loginctl show-user "$USER" | grep Linger查看定时器触发计划:
systemctl --user list-timers --all | grep oc-cleaner卸载:
./oc-cleaner service uninstall提示:Linux 使用
systemd --user。若未开启 lingering,通常需要用户登录会话存在,定时器才会持续运行。
可让自动化平台(或大模型代理)按以下步骤执行:
git clone https://github.com/l2ktech/12-opencode-cleaner.git
cd 12-opencode-cleaner
go build -o oc-cleaner ./cmd/oc-cleaner
./oc-cleaner service install --interval-sec 120 --min-age-min 10 --watch-interval-sec 30 --watch-warn-mem-mb 1024
./oc-cleaner service status如果你希望“后台持续运行 + 开机后自动恢复”,建议再执行:
sudo loginctl enable-linger "$USER"然后重启后验证:
systemctl --user status oc-cleaner-clean.timer --no-pager
systemctl --user status oc-cleaner-watch.timer --no-pager
systemctl --user list-timers --all | grep oc-cleaner如果你只想快速确认“内存超阈值仅提醒”功能,直接执行:
git clone https://github.com/l2ktech/12-opencode-cleaner.git
cd 12-opencode-cleaner
git checkout v0.2.0
go build -o oc-cleaner ./cmd/oc-cleaner
./oc-cleaner watch --warn-mem-mb 1024 --once候选进程需要同时满足:
PPID == 1TTY == ?或??- 运行时长 >=
--min-age-min - 进程没有非
127.0.0.1 / ::1的已建立 TCP 连接
这可最大限度避免误杀正在使用中的前台会话。
当前版本不自动重启高内存会话进程(避免中断工作流)。推荐做法:
- 用
scan先观察状态 - 仅通过
clean回收明确符合条件的孤儿进程 - 按需调小
--interval-sec以提高巡检频率