Bash if command doesn't finish in X time?

Your timeout tag gives it all away:

sudo timeout 60 dhclient $wifi || otherFunction

An example:

sudo timeout 3 sleep 5 || echo finished early

This uses the timeout utility provided by the GNU coreutils package on Linux.


Use timeout.

timeout 2 sleep 1
echo $?
0

timeout 1 sleep 2
echo $?
124

Use the timeout packed as gtimeout with the coreutils port in brew:

brew install coreutils
gtimeout --help

That'll work with /usr/local/bin in your PATH. If you want to use timeout as originally named, add /usr/local/opt/coreutils/libexec/gnubin to your PATH.

Tags:

Bash

Timeout