You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Makefile中timeout命令可通过Ctrl-C中断?

解决Makefile中命令无法被Ctrl-C中断的问题

针对你给出的Makefile:

foo:
    timeout 1m bash -c "while true; do date; sleep 1; done"

无法用Ctrl-C中断的核心原因是:timeout默认会创建新进程组来运行指定命令,导致Ctrl-C发送的SIGINT信号无法传递到bash循环进程中。

以下是两种可行的解决方式:

方法一:给timeout添加--foreground选项

该选项会让timeout不创建新进程组,直接在当前进程组内执行命令,确保SIGINT信号能传递到子进程:

foo:
    timeout --foreground 1m bash -c "while true; do date; sleep 1; done"

方法二:使用GNU Make的.ONESHELL伪目标

指定目标下的所有命令在同一个shell进程中执行,保证信号能正确传递(你的GNU Make 4.3版本完全支持该特性):

.ONESHELL:
foo:
    timeout 1m bash -c "while true; do date; sleep 1; done"

内容的提问来源于stack exchange,提问作者guettli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 06:04:50