41

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?

Glen Selle
  • 3,966
  • 4
  • 37
  • 59
Gary
  • 571
  • 1
  • 4
  • 9
  • http://unix.stackexchange.com/questions/89483/keeping-a-process-running-after-putty-or-terminal-has-been-closed – xthestreams Apr 07 '16 at 03:23
  • I answered to same question at [this line](https://stackoverflow.com/questions/24941791/starting-flask-server-in-background/52968883#52968883) Hope it will help you – Bizzu Oct 24 '18 at 12:27
  • If you just read the title of this question: `FLASK_APP= nohup flask run > log.txt 2>&1 &` – Bálint Sass Apr 05 '23 at 11:47

8 Answers8

40

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.

jdhao
  • 24,001
  • 18
  • 134
  • 273
Gal Mal
  • 421
  • 4
  • 4
  • 3
    Please use the [edit] link to explain how this code works and don't just give the code, as an explanation is more likely to help future readers. See also [answer]. [source](http://stackoverflow.com/users/5244995) – Jed Fox May 23 '17 at 21:21
  • Great answer, works like a charm. It pairs really well with this question/answer on how to shut down the flask server once it is in background mode. in case others were wondering: https://stackoverflow.com/questions/15562446/how-to-stop-flask-application-without-using-ctrl-c – Roman Czerwinski Dec 29 '20 at 20:59
  • It is not able to write all the output to the log.txt. I have given multiple print statements in my app.py however I only get to see some of the one where there is coming error. But when I run via python3 app.py it shows all the print statements. Any suggestions? – Sameer Atharkar Jan 23 '22 at 15:22
  • This works well as long as the computer is on. If someone reboots the computer, you will have to run the same command again. – Erel Segal-Halevi Jan 13 '23 at 08:44
10

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.

Codedreamer
  • 1,552
  • 15
  • 13
9

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

Peixiang Zhong
  • 419
  • 4
  • 8
8

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.

dotmicron
  • 31
  • 5
oz123
  • 27,559
  • 27
  • 125
  • 187
6

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

Hemanth GS
  • 81
  • 1
  • 6
6

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.

Dhrumil Panchal
  • 406
  • 5
  • 11
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

1

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 &
KZiovas
  • 3,491
  • 3
  • 26
  • 47