Terminal 101: Properly Shutting Down Your Mac
Posted 07/02/2012 at 1:41pm
| by Cory Bohon
Every Monday, we'll show you how to do something new and simple with Apple's built-in command line application. You don't need any fancy software, or a knowledge of coding to do any of these. All you need is a keyboard to type 'em out!
Powering off your Mac properly can avoid incurring problems like system file corruption. All it takes is a little patience and the right routine. You can use the Apple menu to turn off your computer, put it to sleep, or restart it, but with Terminal you can select even more options for shutting down your Mac, including logging off all users and setting up certain times for your Mac to shut down. These commands are extremely handy if you manage a server or remote Mac through SSH. Read on to learn more.
Shutting Down

To turn off your Mac, you can use the following command to do so immediately:
sudo shutdown -h now
The “-h” (or “halt") tells the computer to shutdown down. Once issued, the computer will immediately go down after you type in your administrator password. Your user account must have administrator privileges in order to invoke the shutdown commands.
Rebooting

To reboot your Mac, the following command can be used to instantly restart:
sudo shutdown -r now
The “-r” option executes the “Reboot” function in the shutdown command. Once issued, the computer will immediately go down, and will then reboot automatically.
Sleeping

To put your Mac to sleep, or put it in a power saving mode, the following command can be used:
sudo shutdown -s now

Once issued, the computer will enter its power saving mode. If you are using SSH, ensure that the “Wake for network access” option is enabled in System Preferences > Energy Saver, or else the remote computer will be unresponsive for remote access until woken locally by pressing the power button, moving the mouse, or tapping a button on the keyboard.
Using a Timed Shutdown
In the above commands, we used the “now” specifier, which told the computer to begin the shutdown procedure immediately. However, you can also tell the computer to perform any of the above functions at a certain time using the two methods below.
At a certain time

You can have the system shutdown, restart, or sleep at a certain time by replacing “now” in the above commands with a date and time format like this: yymmddhhmm. Where yy is the year, mm is the month, dd is the day, hh is the hour, and mm is the minutes. The time is specified in a 24-hour time format.
So, if for instance, we wanted the computer to restart at 3:30pm on January 1, 2013, we’d issue the following command:
sudo shutdown -r 1301011530
After executing the command, you’ll be given the date and time of the shutdown in your system’s format, along with the process id (pid).
After a set number of minutes

You can also execute a shutdown procedure after a certain number of minutes has elapsed from the current time. To do this, you’ll replace “now” in the above statements with the following format:
+number
Where “number” is the number of minutes after the current time that the computer will execute the command. So, if you wanted to shutdown your Mac in 30 minutes, you’d issue the following command:
sudo shutdown -h +30
Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author, Cory Bohon on Twitter.