Run Flask App [MyGarage] as a Service in Ubuntu
In my previous post I talked about how I created a Flask app and hosted it on AWS Reserved EC2 Instance.
But hosting of the app was more like executing Gunicorn command and thats it. However I needed my app to be running in background also, even if system restarts.
So, I decided to create it as a service and its very simple.
First, I created file mygarage.service under /etc/systemd/system folder
and it contains below code:
[Unit]
Description=Gunicorn instance to serve Flask app: MyGarage
After=network.target
[Service]User=ubuntu
WorkingDirectory=/home/ubuntu/MyGarage
ExecStart=gunicorn -b 0.0.0.0 app:app
[Install]
WantedBy=multi-user.target
And thats it.
All I needed to do next was
sudo systemctl start mygarage
to start the service and
sudo systemctl enable mygarage
to start it whenever machine starts
We can check the status of service by using
sudo systemctl status mygarage
My application is running successfully as a service.
First published on my old blog, Write Code. Be Agile. Go DevOps.