I'm getting more interested in using Terminal as an alternative way to address solutions on my Mac. I have seen this question entitled "Is there a better way to shutdown/restart OSX?".

I would therefore like to know:

How to shut down, restart and sleep my Mac using the Terminal command exclusively ?

up vote 121 down vote accepted

The command you are after is shutdown. This informs all users that the machine is going to be shutdown and tells all apps to close files etc.

The command takes a parameter -h, -r or -s to shut down, restart or sleep the Mac.

The command has to be run as root so you need to use sudo.

e.g. to reboot the machine immediately

sudo shutdown -r now

e.g. to shutdown the machine in 60 minutes

sudo shutdown -h +60
  • 2
    Maybe slightly unrelated to the question and this answer, but for what it's worth this also comes in handy for me to lock my Mac from the Terminal, kind of like hitting Winkey+L in Windows: /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend. I have it aliased in a file that is sourced in my .bash_profile as follows: alias lock='/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend' – rubynorails Jan 13 '16 at 22:31
  • Does it really "tell all apps to close files etc."? I thought it just killed everything? The man page is from 1998 BSD - has Apple updated the command to talk nice to macOS software? Useful if things have hung, but if the system is working well, it seems like telling the loginwindow process to do the deed would be more "polite". – j-beda May 4 at 13:30
  • @j-beda See Apple doc developer.apple.com/legacy/library/documentation/Darwin/… - It first sends a SIGTERM which apps should deal with before they receive a SIGKILL. See the other answer for doing it via the login Window/GUI Actually how would the loginwindow do the deed? – Mark May 4 at 22:22
  • I am pretty sure that the Unix "shutdown" does not do all the stuff that the regular Mac GUI shutdown process does, thus, to the extent that Apple does not use it in their own software directly but at a minimum does some stuff before invoking it, there is some downside to invoking it directly. I believe that the "loginwindow" app is available even when not at the GUI login window, and can be invoked via the command line "osascript" tool. It isn't infallible however, so one might need to invoke "shutdown" anyhow (or send individual kills to specific processes I suppose). – j-beda May 6 at 1:43
  • The signal mechanism does not put a message on the Apps message queue as a normal close does but apps still should deal with it. ie if it does not work properly that is the Apps's fault. for some attempt at GUI see the other answer. But I finds that there is often an app that refuses to die and cancels the quit process so shutdown is the only guranetted way – Mark May 6 at 10:08

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 TERM signal 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 kAEQuitApplication Apple event followed by a KILL signal, and daemons receive a TERM signal followed by a KILL signal after a few seconds.

  • 2
    Comprehensive answer +1 – Tony Cronin Feb 5 '15 at 20:24
  • 1
    It's noted in the QA1134 link, but good to note here that the unattended shutdown and restart commands will fail if any running app fails to respond or cancels the action. For example, if I need to shutdown from the command line, OSX will tell each app one-at-a-time to quit. If that app pops up an "Are you sure?" dialog and there's no response within some amount of time, the system will cancel the shutdown. Also, it won't bother trying to tell any of the remaining apps to quit. – Harvey Feb 24 '15 at 18:01
  • also, I need the -h and the -r options of the terminal shutdown command: setting the time of shutdown, and the later time of reboot, all without having to enter a sudo password. Any idea? – Jean-Denis Muys Jul 23 '15 at 7:23
  • 1
    If an application is blocking your shut down, consider using a third party app like Power Manager. I work on this application and it goes to great lengths to remain Mac friendly. – Graham Miln Jan 9 '16 at 12:23

also useful, to logout from the terminal command line prompt, type 'exit':

[host:~user]$ exit

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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