Skip to content
Advertisement

What is the best way to run a web app using gunicorn at certain hours of the day?

I have a little dashboard that runs with python – dash and I’ve already deployed it successfully in production using GUNICORN.

However, I only want to run it on productive hours (say 8:00 to 20:00). What is the best way to do it? Using crontab to run the GUNICORN launching line? Using crontab to kill the GUNICORN process at the end of the day after launching it with noHup?

Thanks!

Advertisement

Answer

One possible approach would be to add the logic directly in you Dash app, i.e. something like

import dash
import dash_html_components as html
import datetime

def the_actual_layout():
    return html.Div("Hello world!")

def layout():
    hour = datetime.datetime.now().hour
    # If we are outside business hours, return an error message.
    if(hour < 8 or hour > 19):
        return html.Div("The app is only available between 08:00 and 20:00, please come back tomorrow.")
    # Otherwise, render the app.
    return the_actual_layout()

app = dash.Dash()
app.layout = layout

if __name__ == '__main__':
    app.run_server()    

which would thus eliminate the need for external tools to start/stop the app.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement