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 timeout via brew:

$ brew install coreutils

You can see that timeout is part of this package:

$ brew list coreutils | grep timeout

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
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.