timeout
プロセスとシステム
書き方
timeout <duration> <command>コマンドに時間制限をかけ、超えたら打ち切って終了コード 124 を返します。既定のシグナルは TERM で、固まったプログラムは無視できるため `-k` で KILL を続けさせます(GNU coreutils で、macOS には入っていません)。
よく使うオプション
| オプション | 意味 |
|---|---|
| 30 / 5m / 1h | Duration in seconds unless you add s, m, h or d. |
| -s <SIGNAL> | Send something other than TERM when time runs out. |
| -k 5 | If the command ignores TERM, send KILL five seconds later. |
| --preserve-status | Exit with the command's own status instead of 124. |
| exit code 124 | What timeout returns when it had to stop the command. |
例
timeout 30 curl -s https://example.comGive up on a hung request after thirty seconds.
timeout -k 5 60 ./import.shOne 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