Issue
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
Solution
Let's assume all your Flask code resides in the folder /home/abc_user/flask_app
.
Steps
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
Some common operations
- Check current status -
systemctl status flask-server.service
- Start the service -
systemctl start flask-server.service
- Stop the service -
systemctl stop flask-server.service
- Check logs -
journalctl -u flask-server.service
- Stream logs -
journalctl -f -u flask-server.service
- Check logs in past 1 hour -
journalctl -u flask-server.service --since "1 hour ago"
Answered By - Amit Singh Answer Checked By - Gilberto Lyons (WPSolving Admin)