See which app drained my Macbook's battery Sometimes I close my laptop, assuming that there is nothing running that prevents sleep, put it in my bag, and then find the next day that it didn't sleep - instead it ran out of battery and shut down.

Is there any way to see which apps or processes were preventing it from sleeping? It doesn't do this always so I don't want a manual method I have to do before I close the lid.

Note I am not asking which processes are currently draining the battery.
I commented this but realized it is really just a solution. Building off on what @Pratik commented, using the command found here, build a simple bash script to save the output every 2 minutes. You can run this script right before you put the computer to sleep to determine what is keeping your computer awake. If the computer goes to sleep, then this program will be paused, but if it is awake, it will continue to record the process keeping your computer awake. You can change the counter yourself if you find it to to be too much or too less.

Sample Bash Program:

mkdir -p sleep_logs
cd sleep_logs
clear

echo "Starting monitoring programs."

while true
do
    ###Save command output
    pmset -g assertions &gt; $(date +%H.%M.%S_%Y.%m.%d).txt
    echo "Command Saved @ $(date +%H.%M.%S_%Y.%m.%d)"
    wait

    ###Change time here
    sleep 2m
done

echo "Terminated"