Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie I'm going to show you how you can shut down, reboot, and sleep your computer from the Unix command line. Not only can we perform these actions just like we can in the Finder, but from the command line we get an extra cool bonus we can schedule these actions. Now before we get started I want you to proceed carefully. I don't want to be responsible if you put your computer to sleep or reboot and you lose data in a file that you're in the process of editing. So take a moment and make sure that all your files are saved and everything so that if you did accidentally trigger a shutdown you wouldn't lose anything important. Now the command that we're going to use for all three of these, for shut down, reboot and sleep, is going to be the shutdown command. That may not seem intuitive, but that's the command that we use for all three of them and in order to use the shutdown command we always have to use it with sudo.
So sudo shutdown and then immediately after we have to specify one of three options. We have to tell it we want to shut down, reboot or sleep, and we do that with the h, r or s option. h stands for halt, so halt would be to shut down. All three of these options then after that need a time. They want you to specify what time you should shut down and the time format can be one of three things. You can either say shut down now, shut down in a certain number of minutes from now and this case use the plus sign followed by a number, or at a specified date and the date format will be the two last digits of the year followed by two digits for the month, two digits for the date, two digits for the hour, and two digits for the minute.
Now it's a little difficult for me to demonstrate shutting down my computer, so let's just take a look at what some of these might look like. So if we wanted to just do shutdown now the same way that the Finder does it, well then you would do sudo shutdown with the H option for halt and then the word now. If we wanted to schedule it for 45 minutes from now then you could do the same thing but use +45. And if we wanted to shut down at a specified date and time well then you use the date, remember for the year you leave off the first two digits, so it would be 110601 and then zeroes to represent midnight.
Reboot works exactly the same way the only difference is that now we're using the r option instead of the h option. So the first one, reboot now, is the exact same behavior as the Finder has or we can schedule in the future at either a relative or an absolute date. And then for sleep we have the same thing but with the s option. One place that I find this especially useful is that if I'm working late at night, it's almost bed time and I'm doing some long-running process. I'm FTPing a whole lot of files up to a server and its going to take another 30 minutes. Well I can go in to Unix and I can say all right, one hour from now I want my Mac to go to sleep. Then I can go to bed, my file transfer are finish, and then when I'm done my computer will just go to sleep, ready for me to then come back in the next morning.
Now in addition to the shutdown command there also is a Unix command for halt and reboot, but they're not preferable. It's better to use shutdown. So even though you may see halt and reboot in there, shutdown is a better choice. Now for sleep there is a sleep command in Unix, but it's totally different. Because in the days when Unix was embedded computers didn't go to sleep. They were either on or they were off. They didn't have energy-saving modes. So the sleep command in Unix actually just tells Unix to wait for a specified amount of time, so if I say sleep 3, then it waits for three seconds and then it pops back up and give me another command prompt. You can use this with commands.
So sleep 3 and then we could do something hello and wait three seconds and it will do the thing that I asked it to do. So it does have a use especially once we start doing scripting in Unix, but it's completely unrelated from sleeping your computer the way that you're normally used to doing it.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.