How can I find what program is preventing my Mac from going to sleep? My Mac is set to go to sleep after 5 mins of inactivity, and typically this works fine. But every week or so I find that my Mac no longer goes to sleep and nothing I have tried stops this behavior.

So far I have tried:


resetting the SMC and NVRAM a number of times
starting up in Safe Mode
checking that my Energy Saver settings have not changed (Sleep is still set at 5 mins and I have not opted to prevent the Mac from sleeping when the display is off)
quitting all running apps and using Force Quit to relaunch the Finder
updating all my programs to the latest versions
updating macOS to the latest version (currently macOS Mojave 10.14.5)


I have never installed anything to prevent sleep.

The only course of action that works is a full restart, which is highly inconvenient.

How do I work out what program is stopping my Mac from sleeping?
You’ve done a pretty good job so far of troubleshooting this yourself and there are many things that prevent sleep out of the box and by design. 

What I would do next is exactly what you’re asking about - a targeted check to see if an application is what’s stopping your Mac from going to sleep.

To do this, follow these steps:


The next time your Mac isn’t going to sleep, open Activity Monitor
Click on the Energy tab
Now you’ll see a list of applications with a number of columns, one of which should be headed “Preventing Sleep”
Now check to see if any applications have a “Yes” listed in this column


IMPORTANT: If an application has an expansion triangle to the left of its name, make sure you expand it to confirm whether anything is preventing your Mac from going to sleep. For example, web browsers such as Safari often have multiple windows/tabs open and one of these could be preventing your Mac from going to sleep. However, this will not be obvious unless you click on the expansion triangle.


If so, you can force them to Quit by selecting them and pressing optioncommandQ
Now wait to see if your Mac will go to sleep


Notes: 


If you don’t see a “Preventing Sleep” column at Step 3, then go to View > Columns > Preventing Sleep to display it.
If no applications are listed as preventing sleep, then let me know via the comment box below and we'll proceed with other steps.


For people that like command line options - there is an assertions log that shows what is going on - things that make the Mac think you are active, things that will block sleep, etc...

pmset -g assertions


This is the same information that the activity monitor shows, just more detail and you can see items come and go by letting the log run:

pmset -g assertionlog


One nice thing about command line is you can ignore everything that doesn't have "sleep" in it:

pmset -g assertionslog|grep -i sleep