3

We're using a VPN to access certain parts of internal infrastructure from the office; the VPN uses 2FA, so saving credentials and bringing it up with the network when the machine wakes up is not an option.

People take their Macbooks to meetings, and when they do they

  • lose power (battery mode)
  • close the lid

That combination causes the Mac to shut down WiFi, which subsequently frequently tears down the VPN connection (it survives when there's no TLS handshake while the interface is down). Is there a way to stop the Mac from going to sleep (turn off WiFi) when the lid gets closed?

Edit (adding script per request in comments):

#!/bin/bash
export PID=$(pgrep Tunnelblick)
CAFPID=""
while true
do
        if $(pgrep -q Tunnelblick && ifconfig | grep -q 172.30 && [[ $( pmset -g ac | wc -l ) -le 1 ]] && [[ $(ps -ef | grep -v grep | grep -c "caffeinate -t 300 -w $PID") -eq 0 ]]) ; then
                echo "need coffee"
                caffeinate -t 300 -w $PID >/dev/null 2>&1 &
        CAFPID=$(echo $!)
        else
                CAFPID=""
        fi
        if [[ $( pmset -g ac | wc -l ) -gt 1 ]] && [[ -z $CAFPID ]] ; then
                echo "no coffee"
                kill -TERM
                $CAFPID >/dev/null 2>&1
        fi
        sleep 60
done
9
  • 1
    I'm not sure that it's technically possible for the cpu to go into its sleep state while maintaining an active wifi connection. You can use the application NoSleep to keep the cpu on, but be prepared for massive battery loss. Nov 12, 2018 at 19:36
  • Thanks @Wowfunhappy - I'll have a look at the tool.
    – tink
    Nov 12, 2018 at 19:43
  • actually the macOS does that already, opens WiFi in sleep mode, to do some maintenance ! Enable the Power Nap !
    – Ruskes
    Nov 12, 2018 at 21:28
  • @Buscar웃 - it's enabled, doesn't seem to keep it alive.
    – tink
    Nov 12, 2018 at 21:58
  • Most will give you the recommendation to keep your Mac from sleeping, that is fine as long you are on Power plug, but not so good if only on battery. So what is it your are trying to have it do ?
    – Ruskes
    Nov 12, 2018 at 22:14

1 Answer 1

7

You could try this, no need to install extra sofyware

Open Terminal and use

$ caffeinate -s  

or

$ caffeinate -w PID

When used with the -w argument, sleep will be inhibited as long as the process ID PID is active. Once the the process exits, the assertion is released, and the configured sleep behavior will resume when appropriate.

Note also that Process ID's are available from Activity Monitor, or can be listed in the terminal by entering ps -Al

6
  • That's a great suggestion! I may be able to run with this; will have to make a tweak to it, though, and only "caffeinate" if the vpnclient is not only running, but a utun interface exists. No point in draining the battery if it's backgrounded w/o a network running (which is what Tunnelblick does). I'll keep you updated with success/failure and upvote when done ;)
    – tink
    Nov 12, 2018 at 23:45
  • great... hope it works for you
    – Ruskes
    Nov 12, 2018 at 23:54
  • -s Create an assertion to prevent the system from sleeping. This assertion is valid only when system is running on AC power. Ho-hum :}
    – tink
    Nov 13, 2018 at 0:23
  • 1
    Well, your suggestion with the -s (at least according to its documentation) didn't do what you said, but with a bit of scripting and caffeinate -t 600 -w $PID I got what I wanted. Thank you so much. :)
    – tink
    Nov 19, 2018 at 3:34
  • 1
    @tink great find, thanks for coming back and sharing, the t 600 is the timer, in seconds or minutes
    – Ruskes
    Nov 19, 2018 at 3:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .