How to run a script before rendering
You should be able to do this be adding a function to:

bpy.app.handlers.render_pre.append(function_name)

This way whatever function you name will be called before the render happens. Here is a little info on it: link

Make sure your function takes self as a parameter, like so:

function_name(self):
    print("Test")