filename = '{}.pid'.format(os.getpid())

@app.before_first_request
def before_first_request():
    with open(filename, 'w') as file:
        file.write('Hello')

def cleanup():
    pass
How to call a function when Apache terminates a Flask process?