First off, I really love this product and use it for both my printers.
While I know it is always best to do a shutdown of Octopi/Octoprint before removing power, this is not always done or remembered.
Is there anything that can be done to mitigate the issue of powering down the printer (which powers the Raspberry Pi3b+) without a shutdown first and then on next power up the web site part of the system is corrupted. The printer control side I think is working as I use the IO pins to run an external relay which is used to power the printer and the lighting.
I also can SSH into the PI but the web app is dead. It is really frustrating and if there is anything that can be done it would be great.
Pseudo-code for external script ~/scripts/safeShutdown
Shutdown OctoPrint
Wait
Toggle the relay OFF
The trick is to call this outside of the main thread of OctoPrint so that it will continue after that first step.
From your workstation, you can create a public/private keypair, push the public key to the Pi and then you're able to run commands remotely from your own workstation without providing the password.
ssh-copy-id -i ~/.ssh/id_rsa.pub pi@octopi.local # Assuming that you used the defaults for ssh-keygen on your workstation earlier, this pushes the public key over to the Pi
Add a file to /etc/sudoers.d so that the Pi user can run password-less sudo promotion of the ~/scripts/safeShutdown script
ssh -t pi@octopi.local "sudo ~/scripts/safeShutdown" # Remotely runs the script from your workstation, suitable for including in a Bash shell script, for example
And yet, this still doesn't shutdown Raspbian. It only shuts down OctoPrint. A better approach would be to replace your relay with a TP-Link SmartPlug device. Then you would change the third line of the pseudo-code to "Shutdown Raspbian". Your own workstation's Bash script could then toggle off power on the TP-Link device.
Or with the TPLinkSmartplug plugin and a TPLink device, set the options to use countdown timers to power off the plug after a long delay, and the system command option to shutdown raspbian before the delay set in countdown timer.
I use a super-capacitor UPS on my Octoprint Pi. I can just turn the power off and the UPS shuts the Pi down correctly after a minute or so. I've been planning to use a Sonoff so the Pi can shut the printer down. But just turning the printer off is much easier
I'll dig up a link to the UPS
Info available at mfg of the board. [superiam.eu/index.php/shop/ups/raspberry-pi-ups-super-capacitor-backup-power-supply]
Does depend on using a GPIO pin to trigger shutdown and is preset to #18