Skip to content Skip to sidebar Skip to footer

How To Run Flask Server In The Background

I have set up Flask on my Rapsberry Pi and I am using it for the sole purpose of acting as a server for an xml file which I created with a Python script to pass data to an iPad app

Solution 1:

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.

Solution 2:

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

Solution 3:

You have multiple options:

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

$ sudo python app1c.py &

  1. Medium: install tmux with apt-get install tmux launch 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.

Solution 4:

Use:

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

  1. ">> log.txt" pushes all your stdout inside the log.txt file (You may check the application logs in it)

  2. "2>&1" pushes all the stderr inside the log.txt file (This would push all the error logs inside log.txt)

  3. "&" at the end makes it run in the background.

You would get the process id immediately after executing this command with which you can monitor or verify it.

$sudo ps -ef | grep <process-id>

Hope it helps..!!

Solution 5:

I've always found a detached screen process to be best for use cases such as these. Run: screen -m -d sudo python app1c.py

Post a Comment for "How To Run Flask Server In The Background"