Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Mac OS X, is there a programmatic way to get the machine to go to sleep/hibernate?

Tags:

macos

Is there some command line or AppleScript that I can write/run to make the machine go to sleep automatically or even better, into hibernate mode?

I can run the program as root.

like image 251
Uri Avatar asked Sep 06 '25 03:09

Uri


2 Answers

osascript -e 'tell application "Finder" to sleep'
like image 106
Rob Kennedy Avatar answered Sep 07 '25 19:09

Rob Kennedy


To go into Hibernate mode, you'll need to set the correct setting on the power manager.

Possible Settings:

0 - Old style sleep mode, with RAM powered on while sleeping, safe sleep disabled, and super-fast wake.

1 - Hibernation mode, with RAM contents written to disk, system totally shut down while “sleeping,” and slower wake up, due to reading the contents of RAM off the hard drive.

3 - The default mode on machines introduced since about fall 2005. RAM is powered on while sleeping, but RAM contents are also written to disk before sleeping. In the event of total power loss, the system enters hibernation mode automatically.

5 - This is the same as mode 1, but it’s for those using secure virtual memory (in System Preferences -> Security).

7 - This is the same as mode 3, but it’s for those using secure virtual memory.

So after all that, if you want to hibernate automatically and you don't have secure VM turned on then you could run the following shell commands (as a setuid root script):

pmset -a hibernatemode 1; osascript -e 'tell application "Finder" to sleep'
like image 41
Coxy Avatar answered Sep 07 '25 20:09

Coxy