如果命令未在 X 时间内完成,则 Bash?

Phi*_*ide 15 bash timeout

我正在尝试运行类似的东西:

sudo dhclient $wifi || otherFunction
Run Code Online (Sandbox Code Playgroud)

问题是当dhclient失败时它只是挂起而不是抛出错误。

如果在 60 秒内没有完成,我该如何重写上面的内容,以便dhclient被杀死otherFunction并被调用dhclient

Jef*_*ler 17

你的标签让一切都消失了:

sudo timeout 60 dhclient $wifi || otherFunction
Run Code Online (Sandbox Code Playgroud)

一个例子:

sudo timeout 3 sleep 5 || echo finished early
Run Code Online (Sandbox Code Playgroud)

这使用Linux 上的 GNU coreutils 包提供的超时实用程序

  • 不幸的是在 MacOS 上不起作用 (2认同)