在 macOS 上为 fish shell 中的命令添加超时

Add a timeout for a command in fish shell on macOS

在 macOS 上 shell 是否有内置超时命令?

我在 Google 上搜索并看到 bash 的 timeout 命令,但是当我 运行 它时我得到了 fish: Unknown command: timeout

具体来说,我想 运行 一个命令(在本例中为 git status)并添加一个超时,这样如果它花费的时间太长,另一个命令就是 运行。我在想像这样的伪代码,它将 运行 <cmd> 超时为 500 毫秒,而不是 运行 <backup> 如果 <cmd> 超过了指定的时间-t:

$ # usage: timeout [-t <ms>] <cmd> [backup]
$ timeout -t 500 'git status' 'echo "took too long"'

timeout 是在任何 shell 中都有效的普通命令。如果您使用的是 Homebrew,则可以通过 brew install coreutils.

安装它