How to run script on Blender Quit
You might also try the atexit module

import time
import atexit

filepath = r"C:\Users\pink_vertex\Documents\Blender\log.txt"

@atexit.register
def on_exit():
    with open(filepath, "a") as output:
        output.write(time.strftime("Exit on %d.%m at %H:%M:%S\n"))