I know many let their Pi run all the time but as I may go long periods between printer use, an automatic shutdown when printer was powered down was desirable. Of course, if your memory is better than mine you probably could use the shutdown in Octoprint and power down the Pi. I wanted an automatic solution and failed to find one on my own that was practical.
I found the perfect device called PowerBlock to do the job. The only other equipment required is a 5V relay and these are inexpensive and available on Amazon. Its coil is connected to any 5V source on the printer control board. I used an unused end stop input which offers 5V. A extender for the Pi GPIO had to be used because of the heat sink for the cooling. Heatsink fans are now connected to Powerblock voltage output and work fine. There are a couple of felt bumper pads on the bottom of the Powerblock PCB so it cannot short out against the heatsink. Here is a photo of the installation:
When I first tried this, the installation script failed with Octoprint. I contacted Customer Support for PowerBlock and within a few days, a updated script was available and it worked perfectly. This level of customer support is extraordinary. I am happy to answer questions.