Uri
Uri

Reputation: 89729

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

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.

Upvotes: 4

Views: 1108

Answers (4)

chepner
chepner

Reputation: 530970

The common shutdown utility has a -s option to schedule sleep, rather than a restart or shutdown.

$ sudo shutdown -s now

It does not appear to support hibernation, though.

Upvotes: 0

ManuelSchneid3r
ManuelSchneid3r

Reputation: 16091

Stolen from this excellent collection:

Shut down without showing a confirmation dialog:

osascript -e 'tell app "System Events" to shut down'

Shut down after showing a confirmation dialog:

osascript -e 'tell app "loginwindow" to «event aevtrsdn»'

Restart without showing a confirmation dialog:

osascript -e 'tell app "System Events" to restart'

Restart after showing a confirmation dialog:

osascript -e 'tell app "loginwindow" to «event aevtrrst»'

Log out without showing a confirmation dialog:

osascript -e 'tell app "System Events" to  «event aevtrlgo»'

Log out after showing a confirmation dialog:

osascript -e 'tell app "System Events" to log out'

Go to sleep (pmset):

pmset sleepnow

Go to sleep (AppleScript):

osascript -e 'tell app "System Events" to sleep'

Put displays to sleep (10.9 and later):

pmset displaysleepnow

Upvotes: 1

Coxy
Coxy

Reputation: 8928

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'

Upvotes: 3

Rob Kennedy
Rob Kennedy

Reputation: 163257

osascript -e 'tell application "Finder" to sleep'

Upvotes: 9

Related Questions