Reputation: 406
I am using the nohup
command with Python and Flask for background process. After I close the terminal it is working fine but after 1 or 2 days the process stops. Can someone tell me how to keep the background process running? I am using below command:
screen
space
nohup python -m flask run --cert local.crt --key local.key --host=0.0.0.0 --port=443 &
ctrl+a+d
Upvotes: 1
Views: 2391
Reputation:
nohup is a POSIX command to ignore the HUP (hangup) signal. The HUP signal is, by convention, the way a terminal warns dependent processes of logout.
Output that would normally go to the terminal goes to a file called nohup.out if it has not already been redirected.
See nohup.out for searching errors in ./ or executed directory. It is no nohup error. Look nohup.out and google error and will refresh question.
Upvotes: 0
Reputation: 5992
Did you maybe shut down the computer the flask server is running on ? If so, the problem will be solved by either not shutting down your computer or starting the flask server again after shutting down !
Upvotes: 0
Reputation: 3063
Let's assume all your Flask code resides in the folder /home/abc_user/flask_app
.
Create a file flask-server.service
in /etc/systemd/system
.
[Unit]
Description=Flask server
After=network.target
[Service]
User=abc_user
Group=abc_user
WorkingDirectory=/home/abc_user/flask_app
ExecStart=python -m flask run --cert local.crt --key local.key --host=0.0.0.0 --port=443
Restart=always
[Install]
WantedBy=multi-user.target
Run sudo systemctl daemon-reload
.
Start the service using systemctl start flask-server.service
.
Check that it has started by systemctl status flask-server.service
. Status should say "running".
If you want your flask server to auto-start after reboots, run systemctl enable flask-server.service
systemctl status flask-server.service
systemctl start flask-server.service
systemctl stop flask-server.service
journalctl -u flask-server.service
journalctl -f -u flask-server.service
journalctl -u flask-server.service --since "1 hour ago"
Upvotes: 2
Reputation: 116
Try nohup python -m flask run --cert local.crt --key local.key --host=0.0.0.0 --port=443 >/dev/null 2>&1&
Use nohup
, you should redirct you print to /dev/null
or log
, Otherwise it will be create a file nohup.out
occupy disk space.
Most times we use gunicorn
and supervisor
to manager flask application.
Upvotes: 0