You can use an 'Application Handler' to register code to run after certain events in blender, like rendering, saving, loading files and several other situations.
For example, in the code below, appending the function to the save_pre
handler would print out "Saving File" just before the file is saved:
import bpy
def my_handler(scene):
print("Saving File")
bpy.app.handlers.save_pre.append(my_handler)
If you wanted code to execute after the file is saved then you would append to save_post
.
I am not too sure how you would execute code periodically, though there is a scene_update_pre
and scene_update_post
which runs whenever the scene needs to be updated that you could look into.