Mac OS X 上的超时命令?

发布于 2024-09-14 21:46:46 字数 147 浏览 1 评论 0原文

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

例如:

timeout 10 ping google.com

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

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.

e.g:

timeout 10 ping google.com

This program runs ping for 10s on Linux.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

☆獨立☆ 2024-09-21 21:46:46

您可以使用

brew install coreutils

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

gtimeout

.. 代替。为了解释为什么这里有一个来自自制警告部分的片段:

注意事项

所有命令均已安装并带有前缀“g”。

如果您确实需要使用这些命令及其正常名称,您
可以从 bashrc 添加一个“gnubin”目录到你的 PATH 中,如下所示:

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

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

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

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:

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"
允世 2024-09-21 21:46:46

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

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

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

而不是将其放入函数中,您可以将以下行放入脚本中,它也会起作用:

timeout.sh

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

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

timeout.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 [ "$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: 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' "$@";

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

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

Snagged from here:
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

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

or a version that has built in help/examples:

timeout.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 [ "$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: 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' "$@";
酒中人 2024-09-21 21:46:46

正如kvz所述只需使用自制程序:

brew install coreutils

现在timeout命令已经可以使用 - 不需要别名(也不需要 gtimeout,尽管也可用)。

As kvz stated simply use homebrew:

brew install coreutils

Now the timeout command is already ready to use - no aliases are required (and no gtimeout required, although also available).

装迷糊 2024-09-21 21:46:46

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

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

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

ping -t 10 google.com & sleep 5; kill $!
虐人心 2024-09-21 21:46:46

Ubuntu / Debian 的超时包可以在 Mac 上编译并且可以运行。
该软件包位于 http://packages.ubuntu.com/lucid/timeout

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

那片花海 2024-09-21 21:46:46

您可以执行 ping -t 10 google.com >nul

>nul 删除输出。因此,它不会显示 64 BYTES FROM 123.45.67.8 BLAH BLAH BLAH,而是只显示空白换行符,直到超时。 -t 标志可以更改为任意数字。

You can do 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文