macos Mac OS X 上的超时命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3504945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:09:21  来源:igfitidea点击:

Timeout command on Mac OS X?

macostimeout

提问by sheki

Is there an alternative for the timeout command on Mac OSx. The basic requirement is I am able to run a command for a specified amount of time.

Mac OSx 上是否有超时命令的替代方法。基本要求是我能够在指定的时间内运行命令。

e.g:

例如:

timeout 10 ping google.com

This program runs ping for 10s on Linux.

该程序在 Linux 上运行 ping 10 秒。

回答by kvz

You can use

您可以使用

brew install coreutils

And then whenever you need timeout, use

然后每当您需要超时时,请使用

gtimeout

..instead. To explain why here's a snippet from the Homebrew Caveats section:

..反而。为了解释为什么这里有一个来自 Homebrew 警告部分的片段:

Caveats

All commands have been installed with the prefix 'g'.

If you really need to use these commands with their normal names, you can add a "gnubin" directory to your PATH from your bashrc like:

PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"

Additionally, you can access their man pages with normal names if you add the "gnuman" directory to your MANPATH from your bashrc as well:

MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"

注意事项

所有命令都以前缀“g”安装。

如果您确实需要使用具有正常名称的这些命令,您可以从您的 bashrc 中将“gnubin”目录添加到您的 PATH 中,例如:

PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"

此外,如果您也从 bashrc 将“gnuman”目录添加到您的 MANPATH,您可以使用普通名称访问他们的手册页:

MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"

回答by Brad Parks

Another simple approach that works pretty much cross platform (because it uses perl which is nearly everywhere) is this:

另一个几乎可以跨平台工作的简单方法(因为它使用几乎无处不在的 perl)是这样的:

function timeout() { perl -e 'alarm shift; exec @ARGV' "$@"; }

Snagged from here: https://gist.github.com/jaytaylor/6527607

从这里获取:https://gist.github.com/jaytaylor/6527607

Instead of putting it in a function, you can just put the following line in a script, and it'll work too:

您可以将以下行放在脚本中,而不是将它放在函数中,它也可以工作:

timeout.sh

超时.sh

perl -e 'alarm shift; exec @ARGV' "$@";

or a version that has built in help/examples:

或内置帮助/示例的版本:

timeout.sh

超时.sh

#!/usr/bin/env bash

function show_help()
{
  IT=$(cat <<EOF

Runs a command, and times out if it doesnt complete in time

Example usage:

   # Will fail after 1 second, and shows non zero exit code result
   $ timeout 1 "sleep 2" 2> /dev/null ; echo $?
   142

   # Will succeed, and return exit code of 0.
   $ timeout 1 sleep 0.5; echo $?
   0

   $ timeout 1 bash -c 'echo "hi" && sleep 2 && echo "bye"' 2> /dev/null; echo $?
   hi
   142

   $ timeout 3 bash -c 'echo "hi" && sleep 2 && echo "bye"' 2> /dev/null; echo $?
   hi
   bye
   0
EOF
)
  echo "$IT"
  exit
}

if [ "" == "help" ]
then
  show_help
fi
if [ -z "" ]
then
  show_help
fi

#
# Mac OS-X does not come with the delightfully useful `timeout` program.  Thankfully a rough BASH equivalent can be achieved with only 2 perl statements.
#
# Originally found on SO: http://stackoverflow.com/questions/601543/command-line-command-to-auto-kill-a-command-after-a-certain-amount-of-time
# 
perl -e 'alarm shift; exec @ARGV' "$@";

回答by deadmoto

You can limit execution time of any program using this command:

您可以使用以下命令限制任何程序的执行时间:

ping -t 10 google.com & sleep 5; kill $!

回答by sheki

The Timeout Package from Ubuntu / Debian can be made to compile on Mac and it works. The package is available at http://packages.ubuntu.com/lucid/timeout

来自 Ubuntu / Debian 的 Timeout 包可以在 Mac 上编译并且可以工作。该软件包可在http://packages.ubuntu.com/lucid/timeout 获得

回答by redCube

You can do ping -t 10 google.com >nul

你可以做 ping -t 10 google.com >nul

the >nul gets rid of the output. So instead of showing 64 BYTES FROM 123.45.67.8 BLAH BLAH BLAH it'll just show a blank newline until it times out. -t flag can be changed to any number.

>nul 摆脱了输出。因此,它不会显示 64 BYTES FROM 123.45.67.8 BLAH BLAH BLAH,它只会显示一个空白的换行符,直到超时。-t 标志可以更改为任何数字。