How to detect if Mac if rebooting after a power failure using terminal?
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.