Timeout command on Mac OS X?

MacosTimeout

Macos Problem Overview


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.

Macos Solutions


Solution 1 - Macos

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"

Solution 2 - Macos

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' "$@";

Solution 3 - Macos

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

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

Solution 4 - Macos

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

Solution 5 - Macos

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).

Solution 6 - Macos

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshekiView Question on Stackoverflow
Solution 1 - MacoskvzView Answer on Stackoverflow
Solution 2 - MacosBrad ParksView Answer on Stackoverflow
Solution 3 - MacosdeadmotoView Answer on Stackoverflow
Solution 4 - MacosshekiView Answer on Stackoverflow
Solution 5 - MacosjonashacktView Answer on Stackoverflow
Solution 6 - MacosredCubeView Answer on Stackoverflow