Coding

Init Hooks

Try it

管理 OpenClaw 容器启动时自动执行的脚本钩子。gateway 重启、pod 重启、pod 重建均不丢失(数据持久化在 workspace/.init-hooks/)。 支持 inline 命令、本地脚本(shell/python)、CDN URL(zip/tar.gz 自动下载解压)三种钩子类型。 用于:...

What it does

**Version**: 1.0.0 **License**: MIT **Author**: Evan Song · github.com/Songhonglei **Repository**: https://github.com/Songhonglei/better-agent-skills (skills/init-hooks)

The skill document

init-hooks

管理 OpenClaw 容器/主机启动时自动执行的脚本钩子。gateway 重启、容器重启、容器重建均不丢失(数据持久化在 workspace/.init-hooks/)。支持 inline 命令 / 本地脚本(shell·python)/ CDN URL(zip·tar.gz 自动下载解压)三种钩子类型。

路径约定

~/.openclaw/workspace/.init-hooks/
├── hooks.json        # 注册表(truth)
├── dispatcher.sh     # 自动生成,post-init.sh 调用这个
├── last-run.log      # 每次启动执行日志
└── downloads/        # CDN URL 钩子下载缓存
    └── hook_.*

脚本路径:

~/.openclaw/workspace/skills/init-hooks/scripts/
├── manage.py               # 主入口(所有 CRUD + run + install)
└── sync_openclaw_config.py # openclaw.json 多路径同步工具

操作命令速查

所有操作通过 exec 运行,工作目录任意:

SCRIPT="$HOME/.openclaw/workspace/skills/init-hooks/scripts/manage.py"

python3 $SCRIPT install          # 首次安装
python3 $SCRIPT list             # 列出所有钩子
python3 $SCRIPT show         # 查看钩子详情
python3 $SCRIPT add ...          # 新增(见下方)
python3 $SCRIPT edit  ...    # 修改
python3 $SCRIPT enable       # 启用
python3 $SCRIPT disable      # 禁用
python3 $SCRIPT delete       # 删除(需二次确认)
python3 $SCRIPT delete  --force  # 强制删除不确认
python3 $SCRIPT run              # 立即运行所有启用的钩子
python3 $SCRIPT run          # 立即运行指定钩子
python3 $SCRIPT status           # 查看安装状态

新增钩子:三种类型

inline(内嵌 shell 命令)

python3 $SCRIPT add \
  --name "初始化日志目录" \
  --type inline \
  --content "mkdir -p ~/logs && echo 'started' >> ~/logs/boot.log"

多行命令用 \n 分隔:

python3 $SCRIPT add \
  --name "环境变量初始化" \
  --type inline \
  --content $'export MY_VAR=hello\nmkdir -p ~/tmp'

script(本地 shell 脚本)

python3 $SCRIPT add \
  --name "凭证/账号启动恢复" \
  --type script \
  --path "~/.openclaw/workspace/scripts/restore-credentials.sh"

python(本地 Python 脚本)

python3 $SCRIPT add \
  --name "Agent workspace 配置" \
  --type python \
  --path "~/.openclaw/workspace/scripts/set-agents.py"

CDN URL(zip/tar.gz 自动下载解压)

python3 $SCRIPT add \
  --name "远程初始化包" \
  --type script \
  --url "https://cdn.example.com/my-init.zip" \
  --entry "run.sh"         # 压缩包内入口文件名(不指定则自动找第一个 .sh/.py)

首次执行(启动或 run 命令)时自动下载解压到 downloads/hook_/,缓存存在则直接使用,pod 重建后缓存丢失会自动重新下载。

追加 --order 控制执行顺序(默认步进 10)

python3 $SCRIPT add --name "..." --type inline --content "..." --order 5

修改钩子

# 改名称
python3 $SCRIPT edit 2 --name "新名称"
# 改内容
python3 $SCRIPT edit 2 --content "new command"
# 改路径
python3 $SCRIPT edit 1 --path ~/scripts/new.sh
# 改顺序
python3 $SCRIPT edit 1 --order 15
# 改为 URL 模式
python3 $SCRIPT edit 1 --url https://... --entry run.sh

openclaw.json 变更必须同步持久化路径

触发场景: 用户说「修改 openclaw.json」「改 gateway 配置」「给我改配置」,或者用户直接提供了修改 openclaw.json 的脚本/内容。

必须在改完后立即执行:

SYNC="$HOME/.openclaw/workspace/skills/init-hooks/scripts/sync_openclaw_config.py"

# 场景1:Agent 直接 patch(最常用)
python3 $SYNC --patch '{"acp": {"enabled": true}}'

# 场景2:用户提供了修改脚本,执行完后自动同步
python3 $SYNC --run ~/scripts/patch_config.py

# 场景3:inline 命令修改后同步
python3 $SYNC --run-inline "sed -i 's/old/new/' ~/.openclaw/openclaw.json"

# 场景4:只同步(已手动改了 runtime)
python3 $SYNC

# 检查各处是否一致
python3 $SYNC --check

同步路径(跨部署环境可配置):

  1. ~/.openclaw/openclaw.json(runtime,source of truth,始终维护)
  2. 额外持久化目标:由环境变量 INIT_HOOKS_SYNC_PATHS(冒号分隔)配置,不存在的路径自动跳过
# 纯本地/单机:通常无需额外同步,留空即可(只维护 runtime 一份)

# 容器/K8s:把 runtime 之外的持久化挂载点填进来(按你自己的挂载布局替换示例)
export INIT_HOOKS_SYNC_PATHS="/app/clawconfig/openclaw.json:/app/k8s-config/clawconfig/openclaw.json"

未配置 INIT_HOOKS_SYNC_PATHS 时只维护 runtime 一份(本地环境属正常)。

安装流程(install)

install 是幂等的,可重复执行:

  1. 创建 ~/.openclaw/workspace/.init-hooks/ 目录
  2. 初始化 hooks.json(若已存在则跳过)
  3. 生成 dispatcher.sh
  4. 检查 ~/.openclaw/workspace/scripts/post-init.sh
    • 存在 → 在末尾追加 dispatcher 调用(有标记防重复注入)
    • 不存在 → 创建,内容只有 dispatcher 调用
  5. 检查容器启动脚本(/app/start.sh,可用 INIT_HOOKS_START_SH 覆盖;本地无此文件时跳过):
    • 已有 # post-init hook → 间接调用,跳过
    • 否则在末尾追加 dispatcher 直接调用

执行行为

  • 宽松模式:单个钩子失败不中断后续
  • 顺序执行:按 order 字段从小到大
  • 日志:每次执行追加到 last-run.log,同时 tee 到 stdout
  • 执行摘要:每次执行结束后打印成功/失败列表

关于 start.sh 注入

install 会尝试向容器启动脚本(默认 /app/start.sh,可用 INIT_HOOKS_START_SH 覆盖)注入 dispatcher 调用。大多数用户没有写权限、或本地环境根本没有这个文件,都属正常情况,不影响使用:

  • post-init.sh 已注入 → dispatcher 经由 post-init.sh 间接调用
  • 启动脚本注入失败/不存在提示 ℹ️ 而非 ⚠️,无需担心

用户意图识别

用户说Agent 动作
帮我安装 / 初始化 / 设置 init-hooksinstall
添加一个启动钩子 / 加一个开机执行add
列出 / 查看所有钩子list
查看钩子 #2 / 第2个钩子内容show 2
修改钩子 #1edit 1 ...
禁用 / 停用 / 关闭 钩子 #3disable 3
删除钩子 #2delete 2(展示详情后二次确认)
立即运行 / 测试一下 / 跑一遍run
只运行钩子 #1run 1
查看状态 / 安装了吗status
修改 openclaw.json / 改 gateway 配置改完后 sync_openclaw_config.py
查看上次执行日志read ~/.openclaw/workspace/.init-hooks/last-run.log

钩子编写规范

详见 references/best-practices.md,包含三种类型的完整示例、路径规范、执行顺序建议。

核心规则: 钩子内容禁止硬编码 /home/node/,必须用 $HOME(shell)或 Path.home()(Python)。


注意事项

  • 每次 add / edit / enable / disable / delete 后,dispatcher.sh 自动重新生成
  • CDN URL 钩子:首次 run 时下载,缓存在 downloads/;re-install 不清除缓存(需手动删 downloads/hook_/
  • delete --force 跳过确认,Agent 不应使用(除非用户明确说"直接删")
  • hooks.json 是 truth,dispatcher.sh 是生成物,手动改 dispatcher.sh 会在下次操作时被覆盖

Related skills

Find why your productivity system keeps failing, then apply the smallest fix — capacity math, bottleneck routing, durable local notes.

by Iván1 installs

Stores durable facts in a categorized, plain-markdown vault on disk, alongside your agent's built-in memory.

by Iván1 installs

Generate and edit Draw.io, Mermaid, and Excalidraw diagrams from natural language using a structured JSON spec.

by nssa.io1.0k installs47 stars

Run Git operations — commits, branches, merges, rebases, conflict resolution, and recovery — with safety rules enforced.

by Iván532 installs31 stars

Fetch raw ad creative, app, ranking, and revenue data from AdMapix as structured JSON.

by fly0pants

More from songhonglei

Browse all skills

Recover lost agent session content and file changes from on-disk conversation logs. Streaming and OOM-safe on 700MB+ daily JSONL. Two commands: search.py for keyword search across recent sessions (with hit snippets, file-op listing, JSON for agent consumption); extract.py for pulling full write/edit content from a single session by ID prefix, with optional replay-rebuild for pure-edit sequences and safe restore-to-disk (refuses silent overwrites without --yes). Multi-agent aware via --agent main|all|a,b. Configurable data root via --root flag or SESSION_RECOVERY_ROOT env var (default ~/.openclaw/agents/). Trigger when user wants to find lost session content, recover files written by an agent, locate which session modified a file, search session history by keyword, or rebuild a file from an edit replay. Also triggers on 找回会话, 会话被覆盖, 历史会话搜索, 文件被删了, session 丢了, 找回某个文件, 重放编辑.

by blqbzf

小红书图文笔记自动发布技能。通过 ego-browser 自动化完成图片上传、标题填写、正文编辑、 话题标签、发布等全流程。附带 28 种多样式风格卡片生成器(含 3 种照片背景氛围主题), 卡片主题、布局、背景图、遮罩强度、模糊、颗粒等参数均可自由配置。 当用户要求发小红书、发布图文笔记、上传到小红书、小红书发帖或涉及小红书内容发布时触发此技能。 前置依赖:ego-browser (ego-lite) 已安装且正在运行,小红书账号已登录。

by songhonglei2 installs1 stars

Guided token optimization for AI agent workspaces. Triggers on phrases like "save tokens", "optimize tokens", "context window too large", "memory files too b...

by songhonglei2 installs1 stars

将 Markdown/纯文本内容智能分析、排版并导出为高分辨率精美长图或分享海报(内置 18 种视觉风格)。 当用户说「生成长图」「做海报」「文字转图片」「高颜值排版」「做大图」「make a poster」 「export as image」「generate image from text/markdown」等意图时触发本技能。

by songhonglei2 installs

Generic skill-quality auditor for any agent skill (Claude, OpenClaw, Cursor, etc.). Runs a 7-dimension static analysis (D1 process closure & idempotency, D2 tool/command conventions, D3 portability & defense, D4 skill usability, D5 security & op risk, D6 code & doc quality, D7 dependency & footprint) with explicit ERR / WARN severity, 120-point scoring (pass line 90 + zero ERR), and an opt-in `--fix` workflow that always backs up first. Two depths: L1 static (~2 min) and L2 dryRun (~5 min, read-only hub + reachability checks). Strict red lines — read-only by default, never executes the audited skill's writes. Use when the user asks to "audit a skill", "check skill quality", "is this skill ready to ship", "lint my skill", or runs this tool by name. Triggers also: "审计这个 Skill"、"检查 Skill 质量"、"Skill 能上线吗"、 "skill-deep-audit"、"审一下 xxx skill"。

by songhonglei1 installs1 stars

Turn any content — Markdown, plain text, images, tables, Excel (.xlsx), Word (.docx) — into a polished single-page HTML, pick one of 19 built-in themes, set the page Title and FavIcon, then publish it as a live page. When the input is already a well-designed HTML page (or an image with a clear layout), it switches to "layout inheritance" mode: the original layout is kept intact and only the colour theme is swapped, instead of tearing it apart and rebuilding. Use when the user says "turn this into a web page", "make this an HTML page", "convert this doc/table/Excel to HTML", "make it pretty", "把这个转成网页", "做成 HTML 页面", "内容转 HTML".

by songhonglei3 installs