What is the correct way to leave gunicorn running?

2019-01-31 21:11发布

问题:

I want to make a Flask+Nginx+Gunicorn deployment. I have Nginx setup and running and I run gunicorn as described in the docs:

gunicorn app:app

But when I logout of the server the gunicorn process exits? What is the correct way to make sure it stay running for Nginx to connect to, and restarts if it crashes?

回答1:

I'd look into something like Supervisor.



回答2:

Use --daemon option while running gunicorn. Example:

gunicorn grand56.wsgi:application --name grand56 --workers 3 --user=root --group=root --bind=127.0.0.1:1001 --daemon



回答3:

The key thing to note is that when you start the process from the command line it is a child of your terminal process (i. e. a child of bash). When you log out of the server your bash process is terminated - as are all its children.

You'll want to use whatever system you have in place to manage nginx also manage gunicorn (anything from init.d or Upstart scripts to specialized application process monitors like Monit, Supervisor, Bluepill, Foreman, etc.)



回答4:

Pay attention to Sean.

However you can run it on the fly like this:

nohup gunicorn -c config.py </dev/null >/dev/null 2>&1 and it will no longer be dependent on the terminal connection. You could replace >/dev/null with something like >somelogfile if you want to save any output.

But for production use it is best to get it integrated into whatever tool you use for managing processes.



回答5:

use --daemon to the binding command of gunicorn. ex:

gunicorn --bind 0.0.0.0:8001 your_project.wsgi --daemon


回答6:

Try this:

nohup gunicorn app:app &


回答7:

I tried the systemd option and it worked fine, the link below has my full answer and has all the steps , to invoke your app as a gunicorn service.

https://askubuntu.com/questions/930589/running-upstart-script-on-17-04/1010398#1010398



回答8:

Running hug api like this.

--daemon is to keep the process in background.

--access-logfile to keep request log

--bind=< ip>:< port> Giving IP will allow to access from other systems(If proxy is not needed).

gunicorn <pyscirpt_name>:__hug_wsgi__ --name  caassist -w 4 --access-logfile /var/logs/gunicorn/gunicorn_access.log --daemon --bind=<ip>:<port>