To stop a running flask application in the CLI, you can just simply press ctrl + c. But sometimes this doesn’t work and the work around is to get the process id of flask and use the kill command.

But there is another way of addresssing this using a python function. Just simply add this script to your flask application and invoke the /shutdown endpoint.

The code

from flask import request

def shutdown_server():
    func = request.environ.get('werkzeug.server.shutdown')
    if func is None:
        raise RuntimeError('Not running with the Werkzeug Server')
    func()
    
@app.route('/shutdown', methods=['GET'])
def shutdown():
    shutdown_server()
    return 'Server shutting down...'

Please note that this is for local development only.


Reference: https://stackoverflow.com/questions/15562446/how-to-stop-flask-application-without-using-ctrl-c