Nirdesh Kumawat
Nirdesh Kumawat

Reputation: 406

How to make alive nohup process?

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

Answers (4)

user2618529
user2618529

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

TheEagle
TheEagle

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

Amit Singh
Amit Singh

Reputation: 3063

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"

Upvotes: 2

chseng
chseng

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

Related Questions