在Mac OS X上有没有timeout命令的替代方案。基本要求是我能够在指定的时间内运行一个命令。
例如:
timeout 10 ping google.com
这个程序在Linux上运行ping命令10秒钟。
您可以使用
brew install coreutils
每当您需要超时时,请使用
gtimeout
相反,为了解释这一点,这里有来自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"
ln -s /usr/local/bin/gtimeout /usr/local/bin/timeout
命令,这样只需要启用一个命令(使用别名的方法适用于交互式CLI使用,而不适用于从bash脚本中调用)。 - John Ybrew install coreutils
安装了coreutils,现在timeout
命令可以直接使用,不需要前缀了。 - tdensmore另一种在几乎所有平台上都有效(因为它使用几乎普及的 Perl 语言)的简单方法如下:
function timeout() { perl -e 'alarm shift; exec @ARGV' "$@"; }
从这里获取: https://gist.github.com/jaytaylor/6527607
你可以不将其放入函数中,而是将以下行放入脚本中,它也可以正常工作:
perl -e 'alarm shift; exec @ARGV' "$@";
或者是带有内置帮助/示例的版本:
#!/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 [ "$1" == "help" ]
then
show_help
fi
if [ -z "$1" ]
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: https://dev59.com/i3RB5IYBdhLWcg3wgXdV
#
perl -e 'alarm shift; exec @ARGV' "$@";
./timeout.sh 1 "sleep 2.5" 2> /dev/null; echo $?
。请注意,如果您在子shell中运行它,还可以捕获输出,如下所示:RESULT=$(./timeout.sh 1 "sleep 2.5" 2> /dev/null; echo $?)
。 - Brad Parksfunction timeout() { perl -e 'use Time::HiRes "ualarm"; ualarm shift; exec @ARGV' "$@"; }
(请注意,根据http://perldoc.perl.org/functions/alarm.html,它需要Perl> = 5.8) - gibstderr
输出一个“闹钟”消息。一些快速搜索表明这是shell看到SIGALARM的响应,而不是perl
实际输出的内容。有什么建议可以抑制它(而不会同时抑制所有使用超时运行的脚本的stderr
)? - Jordanwhich timeout # /usr/local/bin/timeout
的确如此。 - Joel Mellon您可以使用以下命令限制任何程序的执行时间:
ping -t 10 google.com & sleep 5; kill $!
ping-t 1 google.com&sleep 5;kill $!
我意识到他/她的意思是: 整个表达式将在精确5秒后终止。然而,这是我选择的解决方案,因为我在从Python程序中运行一个bash命令的后台线程——只要它在某个时候终止了,我就很满意了。 - Mark来自Ubuntu/Debian的Timeout包可以在Mac上编译并运行。该软件包可以在http://packages.ubuntu.com/lucid/timeout找到。
brew install coreutils
- 然后使用gtimeout
或者将您的PATH设置为使用timeout
名称。 - RichVel你可以执行 ping -t 10 google.com >nul
>nul 命令会将输出清除。所以,不会显示“64 BYTES FROM 123.45.67.8 BLAH BLAH BLAH”,而是在超时之前只会显示一个空白的新行。-t 标志可以更改为任何数字。