Reputation: 13091
I have a running Flask app that is working fine on a Linux (CentOS) server.
To make it work in the background I started it within a screen
session. There is scheduled downtime for the server every night. The next day all screen sessions are gone. How can I keep my Flask app or screen sessions always up and running after reboots?
Upvotes: 1
Views: 1959
Reputation: 1
Create shell script(/home/newuser/myserver.sh) to run the flask server.
source /home/newuser/FastAPI_Python/Flask_new_venv/Scripts/activate
export FLASK_APP=/home/newuser/FastAPI_Python/app.py
nohup python3 -m flask run --host=0.0.0.0 --debugger > /home/newuser/log.txt 2>&1 &
Add the same shell script to cron job:
[newuser@master_server ~]$ crontab -e
@reboot /bin/sh /home/newuser/myserver.sh
Note:
FastAPI_Python/Flask_new_venv/Scripts/activate
is the flask application with virtual enviroment configured.
Upvotes: 0
Reputation: 3461
Han Solo gave a good answer in his comment: if you run it as a systemd
service, it'll run on boot, restart if it crashes, and so on.
However, if you just need a quick-and-dirty solution for a couple days while you configure the service properly, you can use cron
:
crontab -e
to edit your crontab fileAdd a line at the bottom that looks like this:
@reboot [put your command here]
Save and exit the editor
sudo reboot
to test it and make sure it worksAnd you're done! The command will now run whenever the system reboots.
Upvotes: 1