OS X: how to keep the computer from sleeping during a ssh session

This is not a out-of-the-box solution but it will possibly work if no one other comes up with a solution :-)

You can manipulate the power management settings with the command pmset. See the manpage for more information about it.

The interesting setting we want to manipulate is sleep:

sleep - system sleep timer (value in minutes, or 0 to disable)

So we can use the following commands:

sudo pmset sleep 25      # go to sleep after 25 minutes
sudo pmset sleep 0       # disable sleep

Now we have to trigger these commands after a login and logut. If I remember this right, Bash is the default shell for Mac OS X which brings us to these two files:

   ~/.bash_profile
          The personal initialization file, executed for login shells
   ~/.bash_logout
          The individual login shell cleanup file, executed when a login shell exits

Edit or create them in your home directory and add the appropriate commands. If you want, save the current sleep value in a temporary file and restore it from it afterwards.

The last problem to solve is the password prompt of sudo. To give your user the permission to invoke pmset without any password, edit your /etc/sudoers with sudoedit. You need to use the NOPASSWD tag. If this is new for you, have a look at the sudoers manual.


This allowed me to use my mac over SSH no matter what, as long as the mac is connected to a power source.

System Preferences, Energy saver, Prevent computer from sleeping automatically when the display is off

Everything gets shut down when the mac gets off its power source, so you don't have to worry about battery life.


OS X's built in pmset utility has a setting that may do what you want. Check the pmset manpage for the ttyskeepawake setting and accompanying explanation.

The following command will probably do what you want:

sudo pmset -c ttyskeepawake 1

The -c flag makes this setting active specifically when the computer is running on AC power (as opposed to battery or UPS power), and ttyskeepawake 1 prevents the computer from sleeping during an active tty session (presumably a remote one). It will allow sleep if the session has been inactive for longer than the sleep timer, but if that's an issue, maybe the computer shouldn't be set to sleep at all.