[PYTHON] Persist Flask API server with forever

Environmental information

environment


Raspi 3B+
OS rasbian (stretch)
pip 20.2.4
python 3.5
npm 6.14.9
forever v3.0.2
flask 0.12.1

Various installations

$ pip install Flask
$ npm install -g forever

Set up a server in Flask

(Reference) https://qiita.com/tomboyboy/items/122dfdb41188176e45b5

hoge.py


from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello world'

if __name__ == '__main__':
    app.run(debug=False, host='0.0.0.0', port=5000)

Run(test)


$ python hoge.py
^C

troubleshoot

Persistence

If this is left as it is, the process will end when exiting ssh, so make it persistent with forever. For forever, the command is node by default, and you can specify any command by using the -c option, and you can also make Python code persistent.

Perform persistence


$ forever start -c python hoge.py

See below for forever commands and options https://qiita.com/disc99/items/57490f5eef3e2eb685ba

Run forever on reboot

Even with the above, if you reboot the Raspberry Pi itself, forever will not be executed at startup, so the server is dead.

Write the following in /etc/rc.local (Reference) https://584homes.com/it/raspberry-pi/rasberrypi-startup1803.html%20=

rc.local



sudo forever start -a --uid Hoge -c python /home/pi/hoge.py

exit 0

Recommended Posts

Persist Flask API server with forever
API with Flask + uWSGI + Nginx
Creating a Flask server with Docker
[Python] Quickly create an API with Flask
Flask can't be RESTful with azure API Apps
Try slack OAuth authentication with flask (Slack API V2)
Launch a web server with Python and Flask
Create an API server quickly with Python + Falcon
Tftp server with Docker
Create an animated GIF local server with Python + Flask
Build API server for checking the operation of front implementation with python3 and Flask
Full-scale server made with Nginx + uWSGI + Flask + Ubuntu (installation)
Try to make RESTful API with MVC using Flask 1.0.2
IP restrictions with Flask
A server that echoes data POSTed with flask / python
Extrude with Fusion360 API
Proxy server with Docker
Full-scale server made with Nginx + uWSGI + Flask + Ubuntu (implementation)
Hello world with flask
Local server with python
Programming with Python Flask
[Vagrant] Set up a simple API server with python
I made LINE-bot with Python + Flask + ngrok + LINE Messaging API
Build a speed of light web API server with Falcon
[Study memo] Build GeoJSON vector tile server with Fast API
Until API made with Flask + MySQL is converted to Docker
Image download with Flickr API
Deploy Flask with ZEIT Now
Use Trello API with python
Touch Flask + run with Heroku
Hello World with Flask + Hamlish
Create an API with Django
Use Twitter API with Python
Unit test flask with pytest
SNS Flask (Ajax) made with Flask
Web application development with Flask
Get information with zabbix api
Web API with Python + Falcon
Play RocketChat with API / Python
View flask coverage with pytest-cov
Easy HTTP server with Python
Support yourself with Twitter API
Call the API with python3.
Web application with Python + Flask ② ③
Use subsonic API with python3
File upload with Flask + jQuery
Server management with Jupyter (1) import
Successful update_with_media with twitter API
Web application with Python + Flask ④
Qiita API Oauth with Django
Get ranking with Rakuten API