How to run Flask Server in the background How to run Flask Server in the background python python

How to run Flask Server in the background


Use:

$ sudo nohup python app1c.py > log.txt 2>&1 &

nohup allows to run command/process or shell script that can continue running in the background after you log out from a shell.

> log.txt: it forword the output to this file.

2>&1: move all the stderr to stdout.

The final & allows you to run a command/process in background on the current shell.


Install Node package forever at here https://www.npmjs.com/package/forever
Then use

forever start -c python your_script.py

to start your script in the background. Later you can use

forever stop your_script.py

to stop the script


You have multiple options:

  1. Easy: deattach the process with &, for example:

$ sudo python app1c.py &

  1. Medium: install tmux with apt-get install tmuxlaunch tmux and start your app as before and detach with CTRL+B.

  2. Complexer:Read run your flask script with a wsgi server - uwsgi, gunicorn, nginx.