본문 바로가기

Tips/Mac

Shut down, sleep, restart, display sleep, log out commands on OS X with bash, applescripts.



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

The four letter codes for the Apple events are listed in AERegistry.h.

All System Events commands above send Apple events to the loginwindow process. loginwindow is sent the same Apple events as above when you log out, restart, shut down, or put the the Mac to sleep normally. See Technical Q&A QA1134: Programmatically causing restart, shutdown and/or logout.

According to man shutdown, shutdown -h now and shutdown -r now send processes a TERMsignal followed by a KILL signal.

According to the Daemons and Services Programming Guide, when you tell loginwindow to log out, processes that support sudden termination are sent a KILL signal, and processes that don't support sudden termination are terminated in different ways: Cocoa applications receive the applicationShouldTerminate: delegate method, foreground applications receive the kAEQuitApplication Apple event, background applications receive the kAEQuitApplicationApple event followed by a KILL signal, and daemons receive a TERM signal followed by a KILLsignal after a few seconds.