cron-schedule.vala/SKILL.md
2026-04-15 18:36:04 +08:00

203 lines
5.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
name: cron-schedule
description: 定时任务管理技能。用于创建、查看、管理定时任务和一次性提醒。激活当用户提到"提醒我"、"定时"、"cron任务"、"定时任务"等相关需求时。
---
# 定时任务管理 Skill
## 概述
本技能负责定时任务的**创建、查看、日志排查**。消息发送能力由 `lark-send-message-as-bot` 技能独立提供。
---
## 两种定时任务方式
### 方式一:系统 crontab当前主要方式
适用于需要执行复杂脚本的任务SQL查询、生成报表、发送文件等
**创建任务:**
```bash
# 编辑 crontab
crontab -e
# 添加一行,格式:分 时 日 月 周 命令
# 示例每天9点执行脚本
0 9 * * * /bin/bash /root/.openclaw/workspace/scripts/my_task.sh >> /var/log/my_task.log 2>&1
```
**强制规范:**
1. **必须重定向日志**:所有任务必须添加 `>> /path/to/log 2>&1`,否则执行失败无法排查
2. **必须使用绝对路径**:脚本路径、命令路径都用绝对路径
3. **必须指定环境变量**:脚本开头添加 `export PATH=/root/.nvm/versions/node/v24.14.0/bin:$PATH`
4. **日志位置规范**
- 通用日志放 `/var/log/<任务名>.log`
- agent 专属日志放 `<workspace>/logs/<任务名>.log`
### 方式二openclaw cron适用于简单提醒
适用于一次性定时提醒和简单的周期性通知。
**一次性提醒:**
```bash
openclaw cron add \
--at "2026-04-16T09:00:00+08:00" \
--name "提醒名称" \
--message "⏰ 提醒内容" \
--announce \
--channel feishu \
--to "user:<open_id>" \
--tz Asia/Shanghai \
--delete-after-run
```
**周期性提醒:**
```bash
openclaw cron add \
--cron "0 9 * * 1" \
--name "每周一提醒" \
--message "⏰ 提醒内容" \
--announce \
--channel feishu \
--to "user:<open_id>" \
--tz Asia/Shanghai
```
> ⚠️ **注意**openclaw cron 的 `--to` 参数要求飞书 `open_id``ou_xxx` 格式),不支持 `user_id`。
> 如果需要用 `user_id` 发送消息或发送文件请使用方式一crontab + 脚本),脚本中参考 `lark-send-message-as-bot` 技能。
---
## 查看当前 agent 的定时任务
### 查看系统 crontab 中的任务
```bash
# 查看所有定时任务
crontab -l
# 只看当前 agent 的任务(按 workspace 路径过滤)
crontab -l | grep "workspace/" # 小溪的任务
crontab -l | grep "workspace-xiaoban" # 小伴的任务
crontab -l | grep "workspace-xiaokui" # 小葵的任务
crontab -l | grep "workspace-xiaobian" # 小编的任务
crontab -l | grep "workspace-xiaoyan" # 小燕的任务
```
### 查看 openclaw cron 中的任务
```bash
openclaw cron list # 查看所有 enabled 的任务
openclaw cron list --json # JSON 格式(含详细状态)
```
### Agent 与 Workspace 对照表
| Agent | Workspace 路径 | 脚本目录 |
|-------|---------------|---------|
| 小溪 xiaoxi | `/root/.openclaw/workspace/` | `scripts/` |
| 小伴 xiaoban | `/root/.openclaw/workspace-xiaoban/` | `scripts/` |
| 小编 xiaobian | `/root/.openclaw/workspace-xiaobian/` | `scripts/` |
| 小葵 xiaokui | `/root/.openclaw/workspace-xiaokui/` | `scripts/` |
| 小燕 xiaoyan | `/root/.openclaw/workspace-xiaoyan/` | `scripts/` |
---
## 日志排查
### crontab 任务日志
```bash
# 查看具体任务的日志(路径在 crontab 配置中指定)
tail -50 /var/log/<任务名>.log
# 查看系统 cron 执行日志(确认任务是否被触发)
grep CRON /var/log/syslog | tail -20
```
### openclaw cron 执行历史
```bash
# 查看指定任务的执行历史
openclaw cron runs --id "<任务ID>"
# 查看调度器状态
openclaw cron status
```
---
## 脚本模板
### 标准定时任务脚本模板
```bash
#!/bin/bash
# 任务名称:<描述>
# 执行时间:<cron表达式说明>
# 归属 Agent<agent名称>
set -e
# 1. 环境配置
export PATH=/root/.nvm/versions/node/v24.14.0/bin:$PATH
LOG_FILE="/var/log/<任务名>.log"
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
}
log "任务开始执行"
# 2. 业务逻辑
# ... 在这里写具体的业务操作 ...
# 3. 发送通知(参考 lark-send-message-as-bot 技能)
# 获取 token
APP_ID=$(jq -r '.apps[0].appId' /root/.openclaw/credentials/<agent_name>/config.json)
APP_SECRET=$(jq -r '.apps[0].appSecret' /root/.openclaw/credentials/<agent_name>/config.json)
TOKEN=$(curl -s -X POST "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal" \
-H "Content-Type: application/json" \
-d "{\"app_id\":\"$APP_ID\",\"app_secret\":\"$APP_SECRET\"}" \
| jq -r '.tenant_access_token')
# 发送消息(选择 user_id 或 chat_id
curl -s -X POST "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=user_id" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"receive_id": "<user_id>",
"msg_type": "text",
"content": "{\"text\":\"✅ 任务执行完成\"}"
}' > /dev/null 2>&1
log "任务执行完成"
exit 0
```
---
## openclaw cron 管理命令
| 命令 | 用途 |
|------|------|
| `openclaw cron list` | 查看所有启用的任务 |
| `openclaw cron list --json` | JSON 格式查看(含状态) |
| `openclaw cron add ...` | 创建任务 |
| `openclaw cron rm <ID>` | 删除任务 |
| `openclaw cron enable <ID>` | 启用任务 |
| `openclaw cron disable <ID>` | 禁用任务 |
| `openclaw cron run <ID>` | 手动执行(调试用) |
| `openclaw cron runs --id <ID>` | 查看执行历史 |
| `openclaw cron status` | 查看调度器状态 |
---
## 安全规则
1. **时区**:统一使用 `Asia/Shanghai`
2. **一次性任务**:必须加 `--delete-after-run`,避免过期任务堆积
3. **禁止破坏性操作**:不允许创建执行删除数据、修改配置等危险操作的定时任务
4. **消息发送**:涉及飞书消息和文件发送时,参考 `lark-send-message-as-bot` 技能