Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
Wouldn't you also want to prevent it going to sleep before your SSH session? – SamB Dec 19 '10 at 3:13

3 Answers

up vote 5 down vote accepted

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.

share|improve this answer
would this method work if the ssh connection was only a sshfs mount? – Stefan Sep 8 '10 at 19:51
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 '10 at 20:06

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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