Exploring Your Mac’s Restarts via the Command Line
Curious to know when you last had to restart your Mac, or how often you’ve been rebooting? It turns out that there’s a command line invocation to report on that information. What’s the utility of such details? If you’re troubleshooting flaky hardware, perhaps you want to document how often you’re having to restart, or you might want to look up the exact date of a recent kernel panic when working with tech support.
If you’re intimidated by Unix, don’t worry, as this one is as simple as it gets. Open Terminal from /Applications/Utilities
, and type last reboot
and press Return. You’re presented with a log of all the times you restarted your Mac, back to when the log begins. Mine dates only to October 2013, whereas Adam Engst’s includes 70 restarts since May 2013, and shows several instances when he was restarting repeatedly while trying to isolate a bad DIMM.
Technically, the last
command displays the sessions of specified users, so you could also type last shortname
— where shortname
is the short name of a user account — to see all the times that particular account logged in, or just last
by itself to show all sessions. reboot
is a pseudo-user, as is shutdown
, so last shutdown
displays all the logged shutdown events.
Want to create a permanent record of your restarts? Try this command:
last reboot > ~/Desktop/reboot-log.txt
That tells your Mac to run the last reboot
command, and then send its output to a new file called reboot-log.txt
on your Desktop. And if you wanted to keep recording restarts to this file from the command line, you would instead use:
last reboot >> ~/Desktop/reboot-log.txt
That’s the same command, but with two angle brackets, which specify that the output should be appended to the end of the existing file, instead of overwriting the one that’s already there. Give it a try to see what it does.
While on the subject, as long as you’re in Terminal, try the uptime
command to see how long your computer has been running since the last restart.
There must be more to the "last reboot" command than the article states. I tried it on an iMac running Mavericks and a mini running Leopard. all I get is something like "wtmp begins Mon Feb 17 15:24". Nothing else.
Try just typing "last" and see what it gives you. Also try "man last" which fully describes the command and all of it's options.
Both "last" and "last reboot" return the same disappointing result for me. The man page was no help.
Interesting. From what I understand, these commands are extracting information from your console log, so if you have something that deletes logs regularly, that might explain it. wtmp was the old place this information was maintained before 10.5. Use man to look at the old wtmp and new utmpx for more details.
I verified that /var/run/utmpx exists. Then my eyes glazed over and I fell into a coma.
UPDATE: on my mini running Leopard, "last reboot" does nothing useful, but "last | grep reboot" works. But this does NOT work on my Mavericks iMac. Strange.
ANOTHER UPDATE: I decided to Restart just to see if anything shook loose. After the Restart "last reboot" showed a record for that Restart only. Before that, my uptime had been about three weeks.
It'd be interesting if you could have it append to specific logs when the system boots or shutdown. On Linux, you'd do it by adding a short startup script and shutdown script under /etc/init.d and /etc/rc3.d. However, the Mac, you need to work with launchd which is a bit more involved.
In my case I got the essentially empty result (like several others) running in my default [non-admin] account but a list going back to November when I ran from an admin account. Presumably the cutoff is when the OS was last installed/updated. Was Adam using a machine not updated to Mavericks?
Yep, that must be it. On my Mac Pro still running Mountain Lion, I go back to May, but on my MacBook Air running Mavericks, my log stops at October. So installing a new version of Mac OS X must clear the log.
last reboot >> ~/Desktop/reboot-log.txt
What is this supposed to achieve? Every time it's run it will append the entire history to a file that contains the previous history. You'll end up with tons of duplicate lines if you run this command a lot. It's not as if >> magically only adds new lines to the file.
Yeah, I considered taking that out when editing, but decided that it was mostly just a general Unix tip, even if it wouldn't necessarily be all that useful in this particular context.
Is there a way to get this to run automatically? I know how to add Terminal to my Login Items, of course, but I don't know how to get it to run a command. Please forgive my iggerance.
I'm sure you could do it via Automator, which can encapsulate a shell script in a Mac application, or even with Keyboard Maestro, but why would you want to run it automatically?
To automatically maintain a running log without having to manually add each entry. Thanks for the tips, which I should have thought of myself but was having a dumb day.
It seems that the logs are maintained except for major OS X upgrades, so you can use last reboot at any time to see the running log without having to do anything else. You might want to make a note to save it out to a file before you upgrade to the next version of OS X, though.
Well, I meant "without having to manually add each entry in order to have a continually up-to-date log." Sure, it seems to create a full log back to last OS upgrade when I run the command, but I wouldn't mind being able to just open up that file in Bean and read the most recent entries without having to launch Terminal and run the command first. Call me lazy. Anyway, no biggie. Thanks!
You could run the command as a shell script. Create a file called reboot-log.sh and put this in it:
#!/bin/sh
last reboot >> ~/Desktop/reboot-log.txt
Then, from Terminal, run chmod +x on reboot-log.sh to make it executable. Double click it, and it should create the file on the Desktop. Then just set that shell script to be one of your login items.
Is there a PDF of everything one can do in Terminal?
John, there are entire books that have been written about everything you can do in Terminal. :-)
You might check out our "Take Control of the Mac Command Line with Terminal" ebook.
http://www.takecontrolbooks.com/command-line