Udderings

Udderings is compiled by David Hall, a PhD student in BME at Boston University

More About Me

Change Congress

How to make a Mac sleep via SSH

caseyliss:

So now that I knew how to wake my MacBook remotely, I needed a way to make it go to sleep when I’m done using it.  Doing a little bit of Googling and playing, I’ve come up with the following shell script, which I’ve called ‘gotosleep’:

#!/bin/bash
echo Going to sleep in 30 seconds…
sleep 30
osascript -e ‘tell application “System Events” to sleep’

However, if I issue the command, and then logout while it is waiting on the sleep command, the osascript command won’t execute.  Thus, I need to run my script like this:

nohup ~/gotosleep &

nohup instructs OS X to continue running the command even if my terminal session ends.  To make things easier, I aliased the above in my bash_profile:

alias gotosleep=’nohup ~/gotosleep &’

So I can just type ‘gotosleep’ in any directory, logout, and know the MacBook will go to sleep 30 seconds later.

awesome tip, but one thing to add.  nohup creates nohup.out files wherever you are, so if you don’t want to spew these in your directory whenever you run this, you need to redirect the output of ~/gotosleep to somewhere other than the terminal.  A typical place to redirect is /dev/null.  This would be your alias:

alias gotosleep=’nohup ~/gotosleep>/dev/null &’

Now you can type gotosleep and not spew files all over the place.