There are a number of long winded threads on the Nexus product forums about this. Some people get resolution by replacing or fixing the power button, as noted already here. Others by replacing the battery, others by sending to LG for replacement. I saw at least two reports of cures by rooting and going to an older kernel (5.0, 5.1).
The fact that so many of the reports cluster around 5.1.1 update make me suspicious that the hardware approach (button, battery) address the true problem, but hey if it lets you get another few months or year out of the device go for it.
For me my phone seems be fixed (only 24 hours free and clear so far), but I don't know what the magic was. What I did follows, the presence of next step indicates the previous one didn't work (phone still rebooted or shutdown spontaneously).
- Reset to factory default using Settings menu
- Reset to factory defaults using Recover Mode in bootloader
- Use WugFresh Nexus Root Kit to unlock bootloader, root the phone
- WugFresh revert: Flash stock image and unroot (which uses the most recent official 5.1.1 image)
At this point the phone finally stabilized, going ~15 hours without a reboot or shutdown.
In the middle I talked to Google and LG customer service, I'll add details of that to another answer. Since I'm so deep in now I've since re-unlocked and rooted and applied PureNexus ROM and GAPPS. So far so good, but it's only been a few hours.
Sources:
- Random restarts and "twitchy" power button after Android 5.1.1 update
- Android 5.1 Random Restarts and Optimize App
- or just search the Nexus forum for "random reboot nexus 5" and pay attention to the dozens of threads with hundreds or thousand plus replies...