delayed-command

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Delayed Command Execution

延迟命令执行

Wait for a specified duration, then execute a Bash command.
等待指定时长后,执行Bash命令。

Arguments

参数

  • duration: Time to wait before execution (e.g.,
    30s
    ,
    5m
    ,
    1h
    ,
    1h30m
    )
  • command: The Bash command to run after the delay
  • duration(时长):执行前的等待时间(例如:
    30s
    5m
    1h
    1h30m
  • command(命令):延迟后要运行的Bash命令

Duration Format

时长格式

FormatExampleMeaning
Xs
30s
30 seconds
Xm
5m
5 minutes
Xh
1h
1 hour
XhYm
1h30m
1 hour 30 minutes
XhYmZs
1h5m30s
1 hour 5 min 30 s
格式示例含义
Xs
30s
30秒
Xm
5m
5分钟
Xh
1h
1小时
XhYm
1h30m
1小时30分钟
XhYmZs
1h5m30s
1小时5分30秒

Workflow

工作流程

1. Parse Duration

1. 解析时长

Convert the duration argument to seconds:
  • Extract hours (
    h
    ), minutes (
    m
    ), and seconds (
    s
    ) components
  • Calculate total seconds:
    hours * 3600 + minutes * 60 + seconds
Parsing logic:
bash
duration="$1"
seconds=0
将时长参数转换为秒:
  • 提取小时(
    h
    )、分钟(
    m
    )、秒(
    s
    )分量
  • 计算总秒数:
    小时 * 3600 + 分钟 * 60 + 秒
解析逻辑:
bash
duration="$1"
seconds=0

Extract hours

Extract hours

if [[ $duration =~ ([0-9]+)h ]]; then seconds=$((seconds + ${BASH_REMATCH[1]} * 3600)) fi
if [[ $duration =~ ([0-9]+)h ]]; then seconds=$((seconds + ${BASH_REMATCH[1]} * 3600)) fi

Extract minutes

Extract minutes

if [[ $duration =~ ([0-9]+)m ]]; then seconds=$((seconds + ${BASH_REMATCH[1]} * 60)) fi
if [[ $duration =~ ([0-9]+)m ]]; then seconds=$((seconds + ${BASH_REMATCH[1]} * 60)) fi

Extract seconds

Extract seconds

if [[ $duration =~ ([0-9]+)s ]]; then seconds=$((seconds + ${BASH_REMATCH[1]})) fi
undefined
if [[ $duration =~ ([0-9]+)s ]]; then seconds=$((seconds + ${BASH_REMATCH[1]})) fi
undefined

2. Execute with Delay

2. 延迟执行

For durations up to 10 minutes (600 seconds):
Run synchronously using the Bash tool with appropriate timeout:
bash
sleep <seconds> && <command>
Set the Bash tool's
timeout
parameter to at least
(seconds + 60) * 1000
milliseconds.
For durations over 10 minutes:
Run in background using
run_in_background: true
:
bash
sleep <seconds> && <command>
Inform the user the command is running in background and provide the task ID for checking status.
对于**10分钟以内(600秒)**的时长:
使用Bash工具同步运行,并设置合适的超时时间:
bash
sleep <seconds> && <command>
将Bash工具的
timeout
参数设置为至少
(seconds + 60) * 1000
毫秒。
对于超过10分钟的时长:
使用
run_in_background: true
在后台运行:
bash
sleep <seconds> && <command>
告知用户命令正在后台运行,并提供任务ID用于查看状态。

3. Report Result

3. 结果报告

After execution completes:
  • Display command output
  • Report exit status
  • Note total elapsed time
执行完成后:
  • 显示命令输出
  • 报告退出状态
  • 记录总耗时

Examples

示例

Short Delay (Synchronous)

短延迟(同步执行)

User:
/delayed-command 5m npm test
  1. Parse: 5 minutes = 300 seconds
  2. Execute:
    sleep 300 && npm test
    (timeout: 360000ms)
  3. Report test results
用户:
/delayed-command 5m npm test
  1. 解析:5分钟 = 300秒
  2. 执行:
    sleep 300 && npm test
    (超时时间:360000ms)
  3. 报告测试结果

Long Delay (Background)

长延迟(后台执行)

User:
/delayed-command 1h git commit -m "auto-commit"
  1. Parse: 1 hour = 3600 seconds
  2. Execute in background:
    sleep 3600 && git commit -m "auto-commit"
  3. Return task ID for status checking
用户:
/delayed-command 1h git commit -m "auto-commit"
  1. 解析:1小时 = 3600秒
  2. 后台执行:
    sleep 3600 && git commit -m "auto-commit"
  3. 返回任务ID用于状态查询

Combined Duration

组合时长

User:
/delayed-command 1h30m ./deploy.sh
  1. Parse: 1h30m = 5400 seconds
  2. Execute in background (>600s):
    sleep 5400 && ./deploy.sh
  3. Inform user of background task
用户:
/delayed-command 1h30m ./deploy.sh
  1. 解析:1h30m = 5400秒
  2. 后台执行(超过600秒):
    sleep 5400 && ./deploy.sh
  3. 告知用户后台任务信息

Error Handling

错误处理

ErrorResponse
Invalid duration formatShow supported formats and examples
Missing command argumentPrompt for the command to execute
Command not foundReport error after delay completes
Duration exceeds 24hWarn user and suggest alternative (cron, at)
错误类型响应方式
无效的时长格式显示支持的格式及示例
缺少命令参数提示用户提供要执行的命令
命令未找到延迟结束后报告错误
时长超过24小时向用户发出警告并建议替代方案(cron、at命令)