I want to set a 1 second timeout for networksetup so the command does not take longer than 2 seconds at most.
One approach would be to install the GNU tool
$ brew install coreutils
You can see that
timeout is part of this package:
$ brew list coreutils | grep timeout /usr/local/Cellar/coreutils/8.29/bin/gtimeout /usr/local/Cellar/coreutils/8.29/libexec/gnubin/timeout /usr/local/Cellar/coreutils/8.29/libexec/gnuman/man1/timeout.1 /usr/local/Cellar/coreutils/8.29/share/man/man1/gtimeout.1
And then do this:
$ gtimeout 3 networksetup ....
Here I'm doing a couple of extra things. I'm using
time to show that the command exited after only 3 seconds, I guard the command with a
-k5 which will send a
SIGTERM to the command if it goes past 5 secs.
$ time gtimeout -k5 3 sleep 10 real 0m3.005s user 0m0.002s sys 0m0.003s