How to run Flask Server in the background

时光总嘲笑我的痴心妄想 提交于 2020-08-01 06:11:28

问题


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?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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




回答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




回答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.



来源:https://stackoverflow.com/questions/36465899/how-to-run-flask-server-in-the-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!