# exec: 命令超时

- **ID:** `go/os-exec-command-timeout`
- **领域:** go
- **类别:** system_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

使用 exec.CommandContext 执行的外部命令超过上下文截止时间，导致进程被终止。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.21 | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second); defer cancel(); cmd := exec.CommandContext(ctx, "sleep", "30"); if err := cmd.Run(); err != nil { ... }
   ```

## 无效尝试

- **** — May hang indefinitely, consuming resources. (80% 失败率)
- **** — Doesn't solve the root cause; may still timeout for long-running commands. (60% 失败率)
