118

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 ?

  • reboot for restart also works. – atilkan Feb 7 '16 at 19:21
144

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

From comments there are two things to be addressed

How shutdown works is by sending a sigterm to all processes which should then deal with that e.g. save open files etc. If they don't exit then they will get sent a SIGKILL which forces them to die with no chance to respond. The signals are not sent via the normal key message queue so Apps have to deal with this separately to the code that gets called from quit on the menu. A good app should call common code from both.

This other answer shows how to shutdown as if you hit the menu options. But note that apps can cancel this shutdown

  • 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 '18 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 '18 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 '18 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 '18 at 10:08
92

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
  • @Jean-DenisMuys, if you need to run commands via sudo without being prompted for a password, you can change your permissions for that command with visudo. You can man sudoers for documentation on the configuration file format. – ghoti Jul 27 '18 at 13:36
-3

Shutdown immediately:

**sudo shutdown -h now**

If you do feel like putting a delay on the shutdown, you can use the following command syntax instead:

**sudo shutdown -h +30**

(-h parameter set time to shut down, where (+)30 minutes to stop the system)

  • 1
    The information presented in your answer is already included in the accepted answer, and as such there is no need to repeat what is already mentioned and accepted. – user3439894 Jun 8 at 10:46
-10

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

[host:~user]$ exit
  • 9
    In what way does this shut down, restart or sleep the Mac? – bmike Apr 4 '16 at 13:12
  • Either way, I prefer Ctrl-D to log out, since it doesn't litter the command-line history. – mwfearnley May 28 at 16:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .