delayed-command
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDelayed 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
时长格式
| Format | Example | Meaning |
|---|---|---|
| | 30 seconds |
| | 5 minutes |
| | 1 hour |
| | 1 hour 30 minutes |
| | 1 hour 5 min 30 s |
| 格式 | 示例 | 含义 |
|---|---|---|
| | 30秒 |
| | 5分钟 |
| | 1小时 |
| | 1小时30分钟 |
| | 1小时5分30秒 |
Workflow
工作流程
1. Parse Duration
1. 解析时长
Convert the duration argument to seconds:
- Extract hours (), minutes (
h), and seconds (m) componentss - Calculate total seconds:
hours * 3600 + minutes * 60 + seconds
Parsing logic:
bash
duration="$1"
seconds=0将时长参数转换为秒:
- 提取小时()、分钟(
h)、秒(m)分量s - 计算总秒数:
小时 * 3600 + 分钟 * 60 + 秒
解析逻辑:
bash
duration="$1"
seconds=0Extract 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
undefinedif [[ $duration =~ ([0-9]+)s ]]; then
seconds=$((seconds + ${BASH_REMATCH[1]}))
fi
undefined2. 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 parameter to at least milliseconds.
timeout(seconds + 60) * 1000For durations over 10 minutes:
Run in background using :
run_in_background: truebash
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: truebash
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- Parse: 5 minutes = 300 seconds
- Execute: (timeout: 360000ms)
sleep 300 && npm test - Report test results
用户:
/delayed-command 5m npm test- 解析:5分钟 = 300秒
- 执行:(超时时间:360000ms)
sleep 300 && npm test - 报告测试结果
Long Delay (Background)
长延迟(后台执行)
User:
/delayed-command 1h git commit -m "auto-commit"- Parse: 1 hour = 3600 seconds
- Execute in background:
sleep 3600 && git commit -m "auto-commit" - Return task ID for status checking
用户:
/delayed-command 1h git commit -m "auto-commit"- 解析:1小时 = 3600秒
- 后台执行:
sleep 3600 && git commit -m "auto-commit" - 返回任务ID用于状态查询
Combined Duration
组合时长
User:
/delayed-command 1h30m ./deploy.sh- Parse: 1h30m = 5400 seconds
- Execute in background (>600s):
sleep 5400 && ./deploy.sh - Inform user of background task
用户:
/delayed-command 1h30m ./deploy.sh- 解析:1h30m = 5400秒
- 后台执行(超过600秒):
sleep 5400 && ./deploy.sh - 告知用户后台任务信息
Error Handling
错误处理
| Error | Response |
|---|---|
| Invalid duration format | Show supported formats and examples |
| Missing command argument | Prompt for the command to execute |
| Command not found | Report error after delay completes |
| Duration exceeds 24h | Warn user and suggest alternative (cron, at) |
| 错误类型 | 响应方式 |
|---|---|
| 无效的时长格式 | 显示支持的格式及示例 |
| 缺少命令参数 | 提示用户提供要执行的命令 |
| 命令未找到 | 延迟结束后报告错误 |
| 时长超过24小时 | 向用户发出警告并建议替代方案(cron、at命令) |