So I have a Flask API running through a Systemd service running on a piece of hardware that's battery powered (to control other hardware). I have a bunch of state that I need to save and in case something goes wrong, like a power outage, I need to be able to restore that state.
Right now I save the state as JSON files so I can load them (if they exist) on startup. But I'd also need to be able to remove them again in case it gets the shutdown signal.
I saw somewhere I could set KillSignal to SIGINT and handle the shutdown as a keyboard interrupt. Or something about ExecStop. Would that be enough, or is there a better way to handle such a scenario?
If you look at the shutdown logs of a linux system you'll see 'sending sigterm to all processes... sending sigkill to all processes'.  In a normal shutdown processes get a few second's grace before being killed.  So if you trap sigterm you can run your shutdown code, but it had better be over before the untrappable sigkill comes along.  Since sigterm is always sent to kill a running process, trapping it is indeed the Right Way (TM) to cleanup on exit.  But since you are using systemd services you could also cleanup in the service.
Python handling system shutdown, but not crash (Or how I would like to restore state in case of something unexpected)