首頁·終端機命令

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