首页·终端命令

timeout

进程与系统

用法

timeout <duration> <command>

给命令加上时间上限,超时就掐掉并以 124 退出;默认信号是 TERM,卡死的程序可以不理它,所以要加 `-k` 随后补一个 KILL(属 GNU coreutils,macOS 没有)。

常用选项

选项含义
30 / 5m / 1hDuration in seconds unless you add s, m, h or d.
-s <SIGNAL>Send something other than TERM when time runs out.
-k 5If the command ignores TERM, send KILL five seconds later.
--preserve-statusExit with the command's own status instead of 124.
exit code 124What timeout returns when it had to stop the command.

示例

timeout 30 curl -s https://example.com

Give up on a hung request after thirty seconds.

timeout -k 5 60 ./import.sh

One minute to finish politely, then five seconds before KILL.

看什么在跑、什么在吃机器。先弄清是什么,再决定杀不杀。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. timeout 是做什么的?

给命令加上时间上限,超时就掐掉并以 124 退出;默认信号是 TERM,卡死的程序可以不理它,所以要加 `-k` 随后补一个 KILL(属 GNU coreutils,macOS 没有)。

Q. 怎么写?

timeout <duration> <command> —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 5 个,完整列表在 man timeout。这条命令属于进程与系统。

相关命令

man 手册: man timeout