Gary
Gary

Reputation: 591

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 (iRule).

My RPI is set up as headless and my access is with Windows 10 using PuTTY, WinSCP and TightVNC Viewer.

I run the server by opening a terminal window and the following command:

sudo python app1c.py 

This sets up the server and I can access my xml file quite well. However, when I turn off the Windows machine and the PuTTY session, the Flask server shuts down!

How can I set it up so that the Flask server continues even when the Windows machine is turned off?

I read in the Flask documentation:

While lightweight and easy to use, Flask’s built-in server is not suitable for production as it doesn’t scale well and by default serves only one request at a time.

Then they go on to give examples of how to deploy your Flask application to a WSGI server! Is this necessary given the simple application I am dealing with?

Upvotes: 43

Views: 101413

Answers (8)

KZiovas
KZiovas

Reputation: 4799

I was trying to run my flask app for testing in my GitHub CI and the step where I was running the app was getting stuck for ever. The reason was that it was never releasing the command line

Best solution I found was a combination of two other responses in here:

nohup python script.py &

Upvotes: 2

Codedreamer
Codedreamer

Reputation: 1702

Been stressing lately so I decide to go deep.

pm2 start app.py --interpreter python3

Use PM2 for things like this. I also use it for NodeJs app and a Python app on a single server.

Upvotes: 11

Gal Mal
Gal Mal

Reputation: 441

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.

Upvotes: 42

Dhrumil Panchal
Dhrumil Panchal

Reputation: 446

You can always use nohup to run any scripts as background process.

nohup python script.py

This will run your script in background and also have its logs appended in nohup.out file which will be located in the directory script.py is store.

Make sure, you close the terminal and not press Ctrl + C. This will allow it to run in background even when you log out.

To stop it from running , ssh in to the pi again and run ps -ef |grep nohup and kill -9 XXXXX where XXXX is the pid you will get ps command.

Upvotes: 6

oz123
oz123

Reputation: 28878

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.

Upvotes: 9

Michael Mezher
Michael Mezher

Reputation: 111

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

Upvotes: 5

Hemanth GS
Hemanth GS

Reputation: 81

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..!!

Upvotes: 6

Peixiang Zhong
Peixiang Zhong

Reputation: 439

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

Upvotes: 10

Related Questions