How to run a Python callback when Blender exits? Is it possible to get a callback before Blender closes?

There are bpy.app.handlers. Unfortunately there is no handler for closing Blender.

So is there a way to to know in a script when blender is about to close?
While we could support this via bpy.app.handlers, we decieded not to add this since Python already supports exit callbacks, so you can use these (outside of Blender too - for any Python script).

See Python's atexit module.

Simple example use:

def goodbye(name, adjective):
    print("Goodbye, %s, it was %s to meet you." % (name, adjective))

import atexit
atexit.register(goodbye, "Suzanne", "nice")