I tried all of the tricks mentioned and nothing worked. I finally bought a new battery and carefully replaced it myself. Since then my phone has not been randomly shutting off anymore! The phone also has similar battery life to when I first got it.
(Nexus 5)