167

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 ?

1
  • reboot for restart also works.
    – atilkan
    Commented Feb 7, 2016 at 19:21

4 Answers 4

191

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

7
  • 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' Commented Jan 13, 2016 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
    Commented May 4, 2018 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?
    – mmmmmm
    Commented May 4, 2018 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
    Commented May 6, 2018 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
    – mmmmmm
    Commented May 6, 2018 at 10:08
130

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.

3
  • how about shutting down with applescript after a timeout? I suppose sleep 60 && osascript -e 'tell app "System Events" to shut down' might work but how to do it with applescript only?
    – ccpizza
    Commented Feb 25, 2020 at 21:24
  • So I tried using the applescript technique to do a scripted soft reboot. Problem is, if file sharing users are connected to the machine, it won't reboot. It throws up a "there are users connected..." message, one that does NOT have a timer built in. So is there a way to dump all file sharing users first, before running this command?
    – l008com
    Commented Jan 1, 2021 at 0:01
  • I like the commands without showing a confirming dialog, I think it’s more graceful than halting the system, but, how can I prevents apps to reopen when I log back in? Like unchecking the checkbox from the dialog before shutdown and reboot?
    – panosru
    Commented Oct 23, 2023 at 23:55
-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
  • 8
    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. Commented Jun 8, 2019 at 10:46
-15

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

[host:~user]$ exit
1
  • 19
    In what way does this shut down, restart or sleep the Mac?
    – bmike
    Commented Apr 4, 2016 at 13:12

You must log in to answer this question.

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