Start a flask via systemd service

Viewed 6542

There is a systemd service file '/etc/systemd/system/flask-app.service:

[Unit]
Description="flask-app"
After=network.target

[Service]
User=root
WorkingDirectory=/root/FlaskApp/
Environment=/root/FlaskApp/flask-app/bin
ExecStart=/root/FlaskApp/flask-app/bin/flask run --host=0.0.0.0
Restart=on-failure

[Install]
WantedBy=multi-user.target

After sudo service flask-app start nothing happens, and the systemctl status does not provide any specific information. However, when i run ExecStart command (/root/FlaskApp/flask-app/bin/flask run --host=0.0.0.0) through the console, the service starts.

P.S. I have exported varable FLASK_APP=flask-app.py

P.P.S. My flask app created at venv.

2 Answers

For my Pi3 I used next commands:

  • sudo systemctl start myscript.service (can be started without .service)
  • sudo systemctl status myscript.service (check what's going on)
  • sudo systemctl enable myscript.service (if all is ok this allows autostart)

Here is https://www.raspberrypi.org/documentation/linux/usage/systemd.md little bit more details.

After you create the file, execute

sudo systemctl deamon-reload

Some corrections in your service file.

  1. No need of WorkingDirectory=/root/FlaskApp/ if you are not going to use a relative path for ExecStart= attribute.
  2. Usage of Environment attribute is something different than what you are doing. Check
Related