build-cron

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Build Cron Skill

Build Cron Skill

将用户需求转换为
openclaw cron add
命令并执行。
Convert user requirements into the
openclaw cron add
command and execute it.

安装方法

Installation Method

bash
npx skills add https://github.com/Rinsonlaw/law-skills.git
bash
npx skills add https://github.com/Rinsonlaw/law-skills.git

工作流程

Workflow

第一步:收集信息

Step 1: Collect Information

向用户询问以下信息(不要跳跃,等用户回复后再继续):
  1. 任务名称 — 用户给这个定时任务起的名字,如
    daily-report
  2. 任务描述 — 用户说的原话,直接填入
    --message
    (不要总结,不要改写)
  3. 执行时间 — 用户期望的执行时间,支持四种模式:
    • 定点时间:询问小时(0-23)和分钟(0-59),如"20 30"表示晚上8点30分
    • 间隔循环:询问间隔数值和单位,如"每5分钟"、"每2小时"
    • 周期时间:指定每周几或每月几号,如"每周一9点"、"每月1号0点"
    • 小时范围:指定在哪些小时执行,如"10点到12点每隔1小时"
Session Key 自动获取:自动从当前会话的元数据中获取,不需要用户手动提供。
Ask the user for the following information (do not skip steps; proceed only after the user replies):
  1. Task Name — The name given by the user for this cron job, e.g.,
    daily-report
  2. Task Description — The user's exact words, directly fill into
    --message
    (do not summarize or rewrite)
  3. Execution Time — The user's desired execution time, supporting four modes:
    • Fixed Time: Ask for hour (0-23) and minute (0-59), e.g., "20 30" means 8:30 PM
    • Interval Loop: Ask for the interval value and unit, e.g., "every 5 minutes", "every 2 hours"
    • Periodic Time: Specify a day of the week or day of the month, e.g., "9 AM every Monday", "12 AM on the 1st of every month"
    • Hourly Range: Specify which hours to execute, e.g., "every 1 hour between 10 AM and 12 PM"
Automatic Session Key Retrieval: Automatically obtained from the current session's metadata; no manual input required from the user.

第二步:解析 sessionKey(自动获取)

Step 2: Parse sessionKey (Automatic Retrieval)

自动从当前会话元数据中获取 sessionKey,不需要用户配合。然后用冒号
:
分割,提取以下字段:
session_key = "agent:research:feishu:direct:ou_xxx"
agent_id    = session_key_parts[1]   # → "research"
channel_name = session_key_parts[2]   # → "feishu"
target_id   = session_key_parts[4]   # → "ou_xxx"
Automatically retrieve the sessionKey from the current session metadata without user cooperation. Then split it with a colon
:
to extract the following fields:
session_key = "agent:research:feishu:direct:ou_xxx"
agent_id    = session_key_parts[1]   # → "research"
channel_name = session_key_parts[2]   # → "feishu"
target_id   = session_key_parts[4]   # → "ou_xxx"

第三步:解析执行时间

Step 3: Parse Execution Time

cron 表达式格式说明(从左到右):
位置代表含义取值范围
第1个分钟0-59,
*/N
表示每 N 分钟
第2个小时0-23,
*/N
表示每 N 小时
第3个日期1-31
第4个月份1-12
第5个星期0-6(0=周日)
范围语法:
  • *
    — 每任意值
  • A
    — 指定值
  • A-B
    — 范围,适用于任意位置,如
    10-12
    (小时)、
    5-31
    (日期)、
    2-5
    (星期)
  • */N
    — 间隔,如
    */5
    表示每5个单位
  • A-B/N
    — 范围间隔,如
    10-12/1
    表示从10到12每间隔1
  • 示例:每 5 分钟 →
    "*/5 * * * *"
    (第1个位置为
    */5
  • 示例:每 2 小时 →
    "0 */2 * * *"
    (第2个位置为
    */2
  • 示例:每小时 →
    "0 * * * *"
  • 示例:每周一早上9点 →
    "0 9 * * 1"
  • 示例:每月1号凌晨0点 →
    "0 0 1 * *"
  • 示例:10点到12点每1小时 →
    "0 10-12/1 * * *"
  • 示例:工作日9点到18点每2小时 →
    "0 9-18/2 * * 1-5"
执行时间支持四种模式:
模式 A:定点时间
  • 用户说"每天早上8点"、"晚上8点30分"等
  • 需要解析出小时(0-23)和分钟(0-59)
  • 生成 cron 表达式:
    {minute} {hour} * * *
  • 示例:小时=8, 分钟=0 →
    "0 8 * * *"
模式 B:间隔循环
  • 用户说"每隔5分钟"、"每30分钟"、"每小时"等
  • 需要解析出间隔数值和单位(分钟/小时)
  • 间隔循环的 cron 表达式格式:
    */{interval} * * * *
    (每 N 分钟)或
    0 */{interval} * * *
    (每 N 小时)
模式 C:周期时间
  • 用户说"每周一9点"、"每月1号凌晨0点"等
  • 需要解析出:周期类型(周/月)、具体日期(周一/1号)、小时和分钟
  • 生成 cron 表达式:
    {minute} {hour} {day_of_month} {month} {day_of_week}
  • 星期:0=周日,1=周一,...,6=周六
  • 示例:每周一早上9点 →
    "0 9 * * 1"
  • 示例:每月1号凌晨0点 →
    "0 0 1 * *"
模式 D:定时范围
  • 用户说"10点到12点每隔1小时"、"工作日早上9点到下午6点每隔2小时"等
  • 需要解析出:起始时间、结束时间、执行间隔
  • cron 表达式格式:
    {minute} {start_hour}-{end_hour}/{interval} * *
  • 示例:10点到12点每隔1小时,0分执行 →
    "0 10-12/1 * * *"
  • 示例:9点到18点每隔2小时 →
    "0 9-18/2 * * *"
  • 注意:范围通常指小时范围,间隔单位为小时
判断规则:
  • 如果用户提到"每隔"、"每.*分钟"、"每.*小时",则视为间隔循环模式
  • 如果用户提到"每周"、"每月"、"周几",则视为周期时间模式
  • 如果用户提到"点到点每隔",则视为定时范围模式
  • 如果用户只说时间点(如"8点"、"20:30"),则视为定点时间模式
Cron expression format explanation (left to right):
PositionMeaningValue Range
1stMinute0-59,
*/N
means every N minutes
2ndHour0-23,
*/N
means every N hours
3rdDay of Month1-31
4thMonth1-12
5thDay of Week0-6 (0=Sunday)
Range Syntax:
  • *
    — Any value
  • A
    — Specified value
  • A-B
    — Range, applicable to any position, e.g.,
    10-12
    (hours),
    5-31
    (days),
    2-5
    (weekdays)
  • */N
    — Interval, e.g.,
    */5
    means every 5 units
  • A-B/N
    — Range with interval, e.g.,
    10-12/1
    means every 1 unit from 10 to 12
  • Example: Every 5 minutes →
    "*/5 * * * *"
    (1st position is
    */5
    )
  • Example: Every 2 hours →
    "0 */2 * * *"
    (2nd position is
    */2
    )
  • Example: Every hour →
    "0 * * * *"
  • Example: 9 AM every Monday →
    "0 9 * * 1"
  • Example: 12 AM on the 1st of every month →
    "0 0 1 * *"
  • Example: Every 1 hour between 10 AM and 12 PM →
    "0 10-12/1 * * *"
  • Example: Every 2 hours between 9 AM and 6 PM on workdays →
    "0 9-18/2 * * 1-5"
Execution time supports four modes:
Mode A: Fixed Time
  • User says "8 AM every day", "8:30 PM", etc.
  • Need to parse hour (0-23) and minute (0-59)
  • Generate cron expression:
    {minute} {hour} * * *
  • Example: hour=8, minute=0 →
    "0 8 * * *"
Mode B: Interval Loop
  • User says "every 5 minutes", "every 30 minutes", "every hour", etc.
  • Need to parse interval value and unit (minute/hour)
  • Cron expression format for interval loop:
    */{interval} * * * *
    (every N minutes) or
    0 */{interval} * * *
    (every N hours)
Mode C: Periodic Time
  • User says "9 AM every Monday", "12 AM on the 1st of every month", etc.
  • Need to parse: period type (week/month), specific date (Monday/1st), hour and minute
  • Generate cron expression:
    {minute} {hour} {day_of_month} {month} {day_of_week}
  • Day of week: 0=Sunday, 1=Monday, ..., 6=Saturday
  • Example: 9 AM every Monday →
    "0 9 * * 1"
  • Example: 12 AM on the 1st of every month →
    "0 0 1 * *"
Mode D: Scheduled Range
  • User says "every 1 hour between 10 AM and 12 PM", "every 2 hours between 9 AM and 6 PM on workdays", etc.
  • Need to parse: start time, end time, execution interval
  • Cron expression format:
    {minute} {start_hour}-{end_hour}/{interval} * * *
  • Example: Every 1 hour between 10 AM and 12 PM, execute at 0 minutes →
    "0 10-12/1 * * *"
  • Example: Every 2 hours between 9 AM and 6 PM →
    "0 9-18/2 * * *"
  • Note: The range usually refers to hourly range, and the interval unit is hours
Judgment Rules:
  • If the user mentions "every", "every * minutes", "every * hours", it is considered Interval Loop mode
  • If the user mentions "every week", "every month", "day of week", it is considered Periodic Time mode
  • If the user mentions "every X hours between A and B", it is considered Scheduled Range mode
  • If the user only mentions a time point (e.g., "8 AM", "20:30"), it is considered Fixed Time mode

第四步:获取时区并构造 CLI 命令

Step 4: Get Time Zone and Construct CLI Command

使用以下参数构造
openclaw cron add
命令:
bash
TZ=$(readlink /etc/localtime | sed 's|.*zoneinfo/||')
openclaw cron add \
  --name "{task_name}" \
  --agent "{agent_id}" \
  --session-key "{session_key}" \
  --cron "{cron_expr}" \
  --tz "$TZ" \
  --session "isolated" \
  --wake "now" \
  --message "{task_message}" \
  --timeout-seconds 600 \
  --announce \
  --channel "{channel_name}" \
  --to "{target_id}"
参数说明:
CLI 参数说明默认值
--name
任务名称必填
--agent
从 sessionKey 解析必填
--session-key
完整 session key必填
--cron
cron 表达式必填
--tz
时区自动检测
readlink /etc/localtime
--session
session 模式
"isolated"
--wake
唤醒模式
"now"
--message
任务描述(用户的原话,不做任何修改)必填
--timeout-seconds
超时秒数
600
--announce
启用结果播报到 channel默认关闭
--channel
播报 channel从 sessionKey 解析
--to
播报目标从 sessionKey 解析
注意:时区固定为
"Asia/Shanghai"
,若用户需要其他时区可替换。
Construct the
openclaw cron add
command using the following parameters:
bash
TZ=$(readlink /etc/localtime | sed 's|.*zoneinfo/||')
openclaw cron add \
  --name "{task_name}" \
  --agent "{agent_id}" \
  --session-key "{session_key}" \
  --cron "{cron_expr}" \
  --tz "$TZ" \
  --session "isolated" \
  --wake "now" \
  --message "{task_message}" \
  --timeout-seconds 600 \
  --announce \
  --channel "{channel_name}" \
  --to "{target_id}"
Parameter Explanation:
CLI ParameterDescriptionDefault Value
--name
Task nameRequired
--agent
Parsed from sessionKeyRequired
--session-key
Complete session keyRequired
--cron
Cron expressionRequired
--tz
Time zoneAutomatically detected via
readlink /etc/localtime
--session
Session mode
"isolated"
--wake
Wake-up mode
"now"
--message
Task description (user's exact words, no modifications)Required
--timeout-seconds
Timeout in seconds
600
--announce
Enable result announcement to channelDisabled by default
--channel
Announcement channelParsed from sessionKey
--to
Announcement targetParsed from sessionKey
Note: The time zone is fixed to
"Asia/Shanghai"
; users can replace it if other time zones are needed.

第五步:执行创建命令

Step 5: Execute the Creation Command

将构造好的命令展示给用户确认,用户输入
yes
后执行。
Show the constructed command to the user for confirmation, and execute it after the user inputs
yes
.

完整对话示例

Complete Conversation Examples

定点时间示例:
用户:我想创建一个定时任务,每天晚上8点执行
助手:请问这个任务叫什么名字?
用户:nightly-digest
助手:请问这个任务具体要做什么?(直接填入 message,原话即可)
用户:每晚整理并总结当天的竞品动态
助手:请问执行时间是几点几分?(比如 20 30 表示晚上8点30分)
用户:20 30
助手:(自动获取当前 session key)
     识别为定点时间模式,构造如下命令,请确认是否创建:
TZ=$(readlink /etc/localtime | sed 's|.*zoneinfo/||')
openclaw cron add \
  --name "nightly-digest" \
  --agent "research" \
  --session-key "agent:research:feishu:direct:ou_xxx" \
  --cron "30 20 * * *" \
  --tz "$TZ" \
  --session "isolated" \
  --wake "now" \
  --message "每晚整理并总结当天的竞品动态" \
  --timeout-seconds 600 \
  --announce \
  --channel "feishu" \
  --to "ou_xxx"
是否确认创建?(输入 yes 确认)
用户:yes
助手:执行命令...
间隔循环示例:
用户:我想每5分钟检查一下服务器状态
助手:请问这个任务叫什么名字?
用户:server-health-check
助手:请问这个任务具体要做什么?(直接填入 message,原话即可)
用户:检查服务器状态是否正常
助手:请问执行时间是怎样的?(比如 "每5分钟" 或 "每2小时")
用户:每5分钟
助手:(自动获取当前 session key)
     识别为间隔循环模式(每5分钟),构造如下命令,请确认是否创建:
TZ=$(readlink /etc/localtime | sed 's|.*zoneinfo/||')
openclaw cron add \
  --name "server-health-check" \
  --agent "research" \
  --session-key "agent:research:feishu:direct:ou_xxx" \
  --cron "*/5 * * * *" \
  --tz "$TZ" \
  --session "isolated" \
  --wake "now" \
  --message "检查服务器状态是否正常" \
  --timeout-seconds 600 \
  --announce \
  --channel "feishu" \
  --to "ou_xxx"
是否确认创建?(输入 yes 确认)
用户:yes
助手:执行命令...
Fixed Time Example:
User: I want to create a scheduled task that runs every day at 8 PM
Assistant: What is the name of this task?
User: nightly-digest
Assistant: What does this task specifically do? (Please provide the exact words to fill into message)
User: Compile and summarize the day's competitor dynamics every night
Assistant: What is the execution time in hour and minute? (e.g., 20 30 means 8:30 PM)
User: 20 30
Assistant: (Automatically retrieves current session key)
     Identified as Fixed Time mode, constructed the following command. Please confirm creation:
TZ=$(readlink /etc/localtime | sed 's|.*zoneinfo/||')
openclaw cron add \
  --name "nightly-digest" \
  --agent "research" \
  --session-key "agent:research:feishu:direct:ou_xxx" \
  --cron "30 20 * * *" \
  --tz "$TZ" \
  --session "isolated" \
  --wake "now" \
  --message "Compile and summarize the day's competitor dynamics every night" \
  --timeout-seconds 600 \
  --announce \
  --channel "feishu" \
  --to "ou_xxx"
Confirm creation? (Enter yes to confirm)
User: yes
Assistant: Executing command...
Interval Loop Example:
User: I want to check server status every 5 minutes
Assistant: What is the name of this task?
User: server-health-check
Assistant: What does this task specifically do? (Please provide the exact words to fill into message)
User: Check if the server status is normal
Assistant: What is the execution time pattern? (e.g., "every 5 minutes" or "every 2 hours")
User: every 5 minutes
Assistant: (Automatically retrieves current session key)
     Identified as Interval Loop mode (every 5 minutes), constructed the following command. Please confirm creation:
TZ=$(readlink /etc/localtime | sed 's|.*zoneinfo/||')
openclaw cron add \
  --name "server-health-check" \
  --agent "research" \
  --session-key "agent:research:feishu:direct:ou_xxx" \
  --cron "*/5 * * * *" \
  --tz "$TZ" \
  --session "isolated" \
  --wake "now" \
  --message "Check if the server status is normal" \
  --timeout-seconds 600 \
  --announce \
  --channel "feishu" \
  --to "ou_xxx"
Confirm creation? (Enter yes to confirm)
User: yes
Assistant: Executing command...

注意事项

Notes

  • 必须等待用户回复再继续下一步,不要假设用户提供的信息
  • 如果自动获取 session key 失败,记录错误并告知用户
  • 所有固定值参数已在命令模板中写死,不需要用户输入
  • 命令生成后必须得到用户确认才能执行
  • Must wait for user replies before proceeding to the next step; do not assume information provided by the user
  • If automatic session key retrieval fails, record the error and inform the user
  • All fixed-value parameters are pre-written in the command template; no user input required
  • The generated command must be confirmed by the user before execution