1

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
MaxPowers
  • 5,235
  • 2
  • 44
  • 69
Nirdesh Kumawat
  • 386
  • 2
  • 16
  • 56
  • I suggest you run Flask for two days without nohup to see if it causes an error of some sort. – Mikah Dec 28 '20 at 14:05
  • You should probably use a property daemon, like gunicorn, for this purpose. – Ken Kinder Dec 29 '20 at 22:36
  • @KenKinder please can you explain in detail how to use daemon like gunicorn – Nirdesh Kumawat Dec 30 '20 at 05:40
  • I would recommend using something like a `systemctl` service for such purposes which provide additional support of auto-restarting your service in case it crashes or fails and making sure that it keeps running – Amit Singh Jan 01 '21 at 16:15
  • 1
    `nohup` does not run the process in the background. It simply makes it ignore the `HUP` signal. – Amit Singh Jan 01 '21 at 16:17

4 Answers4

2

Let's assume all your Flask code resides in the folder /home/abc_user/flask_app.

Steps

  1. 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
    
  2. Run sudo systemctl daemon-reload.

  3. Start the service using systemctl start flask-server.service.

  4. Check that it has started by systemctl status flask-server.service. Status should say "running".

  5. If you want your flask server to auto-start after reboots, run systemctl enable flask-server.service

Some common operations

  1. Check current status - systemctl status flask-server.service
  2. Start the service - systemctl start flask-server.service
  3. Stop the service - systemctl stop flask-server.service
  4. Check logs - journalctl -u flask-server.service
  5. Stream logs - journalctl -f -u flask-server.service
  6. Check logs in past 1 hour - journalctl -u flask-server.service --since "1 hour ago"
Amit Singh
  • 2,875
  • 14
  • 30
  • Thanks.I am getting error /etc/systemd/system/flask-server.service:9: Executable "python" not found in path "/usr/local/sbin:/usr/local/ when I run - `systemctl status flask-server.service` – Nirdesh Kumawat Jan 05 '21 at 16:38
  • 1
    Replace `python` by output of `which python` – Amit Singh Jan 05 '21 at 17:26
  • Previous error has been resolved.Now I am getting error `Loaded: loaded (/etc/systemd/system/flask-server.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Tue 2021-01-05 17:37:19 UTC; 5s ago Process: 1418725 ExecStart=/usr/bin/which python -m flask run --cert local.crt --key local.key --host=0.0.0.0 --port=443 (code=exited, status=217/USER) Main PID: 1418725 (code=exited, status=217/USER)` – Nirdesh Kumawat Jan 05 '21 at 17:39
  • It says `(code=exited, status=217/USER)` which means that the user does not exist or you might have made some mistakes when adding it. See [this](https://stackoverflow.com/questions/48176240/how-to-debug-a-failed-systemctl-service-code-exited-status-217-user) – Amit Singh Jan 05 '21 at 18:44
  • Now I am getting error `Loaded: loaded (/etc/systemd/system/flask-server.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Tue 2021-01-05 19:05:37 UTC; 5s ago Process: 1419532 ExecStart=/usr/bin/which python -m flask run --cert local.crt --key local.key --host=0.0.0.0 --port=443 (code=exited, status=1/FAILURE) Main PID: 1419532 (code=exited, status=1/FAILURE)` – Nirdesh Kumawat Jan 05 '21 at 19:14
  • Please check logs via `journalctl` command mentioned in the question and share them. One issue that I see is that you have written python path as `/usr/bin/which python` instead of the output of `which python` which returns `/usr/bin/python` on my system. So I would replace `/usr/bin/which python` by `/usr/bin/python` – Amit Singh Jan 05 '21 at 19:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226890/discussion-between-martin-and-amit-singh). – Nirdesh Kumawat Jan 05 '21 at 19:40
0

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.

chseng
  • 116
  • 4
0

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 !

TheEagle
  • 5,808
  • 3
  • 11
  • 39
0

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.

HailToTheVictor
  • 388
  • 2
  • 8
  • 28