Install
openclaw skills install task-protectionComprehensive task lifecycle management with automatic tracking, failure analysis, and completion feedback. Use when executing recurring system tasks (backups, health checks, news delivery), critical operations (config changes, data sync, deployments), external interactions (email sending, message notifications, API calls), user-delegated one-time tasks, or long-running operations over 1 minute. Provides 9 tool functions, 8 failure type analysis, progress tracking, and automated reporting.
openclaw skills install task-protection核心原则:所有重要任务必须有记忆、追踪、反馈、故障分析机制 —— 闭环管理
#!/bin/bash
source /path/to/task-utils.sh
# 1. 初始化任务
TASK_ID="my_task_$(date +%Y%m%d_%H%M%S)"
task_init "$TASK_ID" "任务名称" "任务描述"
# 2. 开始执行
task_start "$TASK_ID"
# 3. 记录过程
task_log "$TASK_ID" "INFO" "正在执行..."
# 4. 完成任务
task_complete "$TASK_ID" "执行成功"
# 或失败时
task_fail "$TASK_ID" "错误信息" "错误类型"
AI 在回复中说明并创建状态文件:
📋 任务登记:
- ID: task_20260314_0948_write_article
- 名称:撰写文章
- 状态:执行中
✅ 任务完成!
📋 详情:
- 完成时间:09:48
- 耗时:45 秒
- 状态:成功
📄 状态文件:memory/tasks/task_20260314_0948_write_article.json
task-protection/
├── SKILL.md (本文件)
├── scripts/
│ ├── task-utils.sh # 核心工具函数库(9 个函数)
│ ├── ai-task-register.sh # AI 任务自动登记
│ ├── daily-news.sh # 示例:新闻推送
│ ├── system-health-check.sh # 示例:健康检查
│ ├── check-tasks.sh # 示例:任务扫描
│ └── weekly-task-report.sh # 周报统计
├── memory/
│ ├── task-registry.json # 任务注册表
│ └── tasks/ # 任务状态文件目录
├── logs/
│ └── tasks/ # 任务日志目录
└── docs/
├── QUICKSTART.md # 快速上手指南
├── task-trigger-criteria.md # 触发标准
└── ai-task-registration.md # AI 登记指南
task_init "task_001" "发送邮件" "向团队发送周报"
参数:
task_id - 任务 ID(建议格式:类型_YYYYMMDD_HHMMSS_描述)task_name - 任务名称task_description - 任务描述(可选)输出:
memory/tasks/{task_id}.jsontask_start "task_001"
功能:
runningtask_log "task_001" "INFO" "邮件发送中..."
task_log "task_001" "WARN" "网络延迟"
task_log "task_001" "ERROR" "发送失败"
日志级别:
INFO - 正常进度WARN - 可恢复问题ERROR - 失败/异常DEBUG - 调试信息task_stage "task_001" "准备内容" "running"
task_stage "task_001" "准备内容" "done"
task_stage "task_001" "验证结果" "warning"
阶段状态:
running - 进行中done - 完成failed - 失败warning - 警告task_complete "task_001" "邮件已发送,收件人:team@company.com"
功能:
successtask_fail "task_001" "发送超时" "timeout"
故障类型(8 类):
command_not_found - 命令不存在authentication_failed - 认证失败network_error - 网络错误timeout - 执行超时resource_not_found - 资源不存在permission_denied - 权限不足validation_error - 验证失败unknown_error - 未知错误功能:
failedtask_retry "task_001" 3 60 # 最多 3 次,间隔 60 秒
参数:
task_id - 任务 IDmax_retries - 最大重试次数(默认 3)interval - 重试间隔秒数(默认 60)退避策略:
linear - 线性退避(60s, 60s, 60s)exponential - 指数退避(30s, 60s, 120s)fixed - 固定间隔task_status "task_001"
输出:JSON 格式的任务状态
task_list
输出:所有已登记任务列表
{
"taskId": "task_20260314_0948_example",
"name": "任务名称",
"description": "任务描述",
"status": "success|failed|running|pending",
"stages": [
{"name": "准备", "status": "done", "timestamp": "..."},
{"name": "执行", "status": "done", "timestamp": "..."}
],
"logs": ["[2026-03-14 09:48:00] [INFO] 任务初始化"],
"errors": [],
"result": "任务完成",
"duration": 120,
"createdAt": "2026-03-14T09:48:00+08:00",
"startedAt": "2026-03-14T09:48:00+08:00",
"completedAt": "2026-03-14T09:50:00+08:00"
}
#!/bin/bash
source /path/to/task-utils.sh
TASK_ID="health_check_$(date +%Y%m%d_%H%M%S)"
# 初始化
task_init "$TASK_ID" "系统健康检查" "检查 Gateway、磁盘、内存、日志"
task_start "$TASK_ID"
# 阶段 1: 检查 Gateway
task_stage "$TASK_ID" "检查 Gateway" "running"
if systemctl --user is-active openclaw-gateway > /dev/null 2>&1; then
task_log "$TASK_ID" "INFO" "✅ Gateway 运行正常"
task_stage "$TASK_ID" "检查 Gateway" "done"
else
task_log "$TASK_ID" "ERROR" "❌ Gateway 未运行"
task_stage "$TASK_ID" "检查 Gateway" "failed"
fi
# 阶段 2: 检查磁盘
task_stage "$TASK_ID" "检查磁盘" "running"
DISK_USAGE=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
if [ "$DISK_USAGE" -lt 80 ]; then
task_log "$TASK_ID" "INFO" "✅ 磁盘使用:${DISK_USAGE}%"
task_stage "$TASK_ID" "检查磁盘" "done"
else
task_log "$TASK_ID" "WARN" "⚠️ 磁盘使用过高:${DISK_USAGE}%"
task_stage "$TASK_ID" "检查磁盘" "warning"
fi
# 完成
if [ "$ISSUES_COUNT" -eq 0 ]; then
task_complete "$TASK_ID" "系统健康状态良好"
else
task_fail "$TASK_ID" "发现 $ISSUES_COUNT 个问题" "validation_error"
fi
./scripts/ai-task-register.sh "撰写文章" "为同事写鼓励文章" "normal"
AI 在回复中说明并创建状态文件:
📋 任务登记:
- ID: ai_task_20260314_0948_write
- 名称:撰写安慰文章
- 类型:one-time
- 优先级:normal
- 状态:pending
📝 执行中...
✅ 任务完成!
📋 详情:
- 完成时间:09:48
- 耗时:45 秒
- 状态:成功
📄 状态文件:memory/tasks/ai_task_20260314_0948_write.json
task_$(date +%Y%m%d_%H%M%S)# 查看所有任务
task_list
# 查询特定任务
task_status "task_001"
# 查看日志
tail -50 logs/tasks/task_001.log
打开 docs/task-dashboard.html 可视化查看
cat memory/task-registry.json | jq '.tasks'
docs/QUICKSTART.mddocs/task-trigger-criteria.md - 什么任务需要闭环docs/ai-task-registration.md - AI 任务自动登记流程docs/task-protection.md - 详细设计文档docs/task-protection-examples.md - 更多场景示例task_retry "task_001" 3 60
ls memory/tasks/*.json | xargs -I {} jq '.name, .status, .completedAt' {}
task_cleanup 30 # 清理 30 天前的任务
./scripts/weekly-task-report.sh
输出:articles/任务周报 -YYYY-Www.md
包含:
维护者: 虾球 🦐
版本: 1.0
最后更新: 2026-03-14
许可: MIT(免费开源)
有任何问题随时提问!