How to detect if Mac if rebooting after a power failure using terminal? As I asked in the title, how can I check if a Mac is rebooting after experiencing a power failure. It would be preferable to check this using terminal or some other script, as I would like to test this automatically in a script upon system startup.
You can check for the "Shutdown Cause" in the log.


5 is a normal shutdown
3 is pressing the power button
0 is loss of power


In El Capitan and earlier:

cat /var/log/system.log | grep -i "shutdown cause"


In Sierra and newer:

log show --predicate "process == kernel" | grep -i "shutdown cause"


Or....just using awk to get the code:

log show --predicate "process == kernel" | awk '/shutdown cause/ {print $12}'


In Mojave and Catalina, you can issue the command to get a more fine grained approach to the log:

log show --predicate '(process = "kernel") &amp;&amp; (eventMessage CONTAINS "shutdown cause")' --last 48h --style compact


This will process only the last 48 hours and output in a nice "compact" layout.