Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start python flask webserver automatically after booting the system and keep it on till the end

I'm using flask as a webserver for my UI (it's a simple web interface which controls the recording using gstreamer on ubuntu from a webcam and a framegrabber simultaneously / kinda simple player)

Every time I need to run the command "python main.py" to run the server from command prompt manually.

I've tried the init.d solution or even writing a simple shell script and launching it every time after rebooting the system on start up but it fails to keep the server up and running till the end (just invokes the server and terminates it I guess)

is there any solution that could help me to start the webserver every time after booting the system on startup and keep it on and running?

I'd like to configure my system to boot directly into the browser so don't wanna have any need for more actions by the user.

Any Kind of suggestion/help is appreciated.

like image 298
Sina Sh Avatar asked Nov 07 '15 18:11

Sina Sh


1 Answers

I'd like to suggest using supervisor, the documentation is here


for a very simple demo purpose, after you installed it and finish the set up, touch a new a file like this:

[program:flask_app]                                                                  
command = python main.py                                      
directory = /dir/to/your/app                            
autostart = true                                                                
autorestart = true

then

$ sudo supervisorctl update

Now, you should be good to go. The flask app will start every time after you boot you machine.(note: distribution package has already integrated into the service management infrastructure, if you're using others, see here)

to check whether you app is running:

$ sudo supervisorctl status

For production, you can use nginx+uwsgi+supervisor. The flask deployment documentation is here

like image 126
lord63. j Avatar answered Sep 18 '22 11:09

lord63. j