When I ssh into a OS X computer on my network the session lasts until the OS X goes into sleep mode.

Is there a way to prevent this from happening during my SSH session, apart from physically bumping the mouse or typing keys, or manually disabling the sleep function?

EDIT: The ssh session would normally be a simple sshfs mount.

  • Wouldn't you also want to prevent it going to sleep before your SSH session?
    – SamB
    Dec 19, 2010 at 3:13

7 Answers 7


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.

  • 1
    This option is useless on macOS Sierra. My iMac sleeps regardless of this setting, wakes on SSH access, and then sleeps immediately after SSH logout. It's really annoying. Aug 11, 2017 at 9:04
  • This answer finally solved my issue after fiddling around with ~/.ssh/config for hours on a Mojave iMac
    – n1000
    Apr 4, 2019 at 8:01

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:

          The personal initialization file, executed for login shells
          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.

  • 1
    would this method work if the ssh connection was only a sshfs mount?
    – Stefan
    Sep 8, 2010 at 19:51
  • 1
    I'm not sure, but I don't think sshfs will open a login shell, so this won't work with sshfs.
    – echox
    Sep 8, 2010 at 20:06

To use SSH to open a shell on a remote OSX machine and prevent it from sleeping for the duration of the SSH session:

$ ssh -t me@mymacbook caffeinate -i bash

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.


Since Mac OS X 10.8, you should be able to use the caffeinate(8) command.

Try caffeinate -s ssh server.example.com.

  • 9
    The OP is trying to keep the remote Mac (i.e., the ssh server) from sleeping. Unless I am mistaken, your example will only prevent sleep on the client's terminal.
    – Owen
    Jan 14, 2015 at 20:23

I added the following script in .bash_profile

if expr "$(ps -o comm= $PPID)" : '^sshd:' > /dev/null; then
  caffeinate -s $SHELL --login
  exit $?

If the process is a direct child of a ssh daemon, run a same shell via the caffeinate command to prevent the system from sleeping during the ssh session.

  • manpage link now dead?
    – pjc50
    Apr 12, 2021 at 13:37
  • @pjc50 Thanks. I removed the link. Apr 29, 2021 at 10:21

A web search for mac prevent sleep comes up with a lot of good information on utilities and tricks you can use.

However, in your situation, I suspect that what you really want to do is to have a separate ssh session that runs a very simple command-line program that prevents the Mac from sleeping. Apple gives the complete source code in Technical Q&A 1160: Preventing sleep. (Unfortunately, this page is broken on Apple's site at present, but you can pull up a copy from Google's cache.)

I haven't tested to see if this program can be run successfully from an ssh session, but it looks like it could.

Note that sshfs implementations typically don't stay logged into the remote system all the time, and just run a session when they want a file or directory. So you need a separate ssh session, running in a minimized terminal window, to run the insomnia program.

The advantage of this approach is that you don't need to use sudo, or mess with the system power management settings.

You must log in to answer this question.

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