如何將gunicorn作為systemd守護程式運行

如何將gunicorn作為systemd守護程式運行

我使用 Arch Linux。我嘗試準備gunicorn守護程序來連接到nginx。但我仍然收到錯誤

● gunicorn_yogavidya.service - Yogavidya gunicorn daemon
   Loaded: loaded (/usr/lib/systemd/system/gunicorn_yogavidya.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2016-06-01 20:28:39 BST; 9s ago
  Process: 1301 ExecStart=/html/public_html/yogavidya/gunicorn_start.sh (code=exited, status=203/EXEC)
 Main PID: 1301 (code=exited, status=203/EXEC)

Jun 01 20:28:39 ytsejam systemd[1]: Started Yogavidya gunicorn daemon.
Jun 01 20:28:39 ytsejam systemd[1]: gunicorn_yogavidya.service: Main process exited, code=exited, status=203/EXEC
Jun 01 20:28:39 ytsejam systemd[1]: gunicorn_yogavidya.service: Unit entered failed state.
Jun 01 20:28:39 ytsejam systemd[1]: gunicorn_yogavidya.service: Failed with result 'exit-code'.

這是我的服務文件:

[Unit]
Description=Yogavidya gunicorn daemon

[Service]
Type=simple
User=ytsejam
ExecStart=/html/public_html/yogavidya/gunicorn_start.sh

[Install]
WantedBy=multi-user.target

gunicorn_start.sh

#!/bin/bash

NAME="yogavidya"                              #Name of the application (*)
DJANGODIR=/html/public_html/yogavidya/src             # Django project directory (*)
SOCKFILE=/html/public_html/yogavidya/run/gunicorn.sock        # we will communicate using this unix socket (*)
USER=ytsejam                                      # the user to run as (*)
GROUP=webdata                                     # the group to run as (*)
NUM_WORKERS=1                                     # how many worker processes should Gunicorn spawn (*)
DJANGO_SETTINGS_MODULE=yogavidya.settings             # which settings file should Django use (*)
DJANGO_WSGI_MODULE=yogavidya.wsgi                     # WSGI module name (*)

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source /html/public_html/yogavidya/venv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /html/public_html/yogavidya/venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user $USER \
  --bind=unix:$SOCKFILE

怎麼解決這個問題呢?

答案1

哎呀,gunicorn運行如下:

$ gunicorn [OPTIONS] APP_MODULE

而你做到了

$ gunicorn APP_MODULE [OPTIONS]

腳本的最後部分gunicorn_start.sh應為:

exec /html/public_html/yogavidya/venv/bin/gunicorn \
    --name $NAME \
    --workers $NUM_WORKERS \
    --user $USER \
    --bind=unix:$SOCKFILE \
    ${DJANGO_WSGI_MODULE}:application

順便說一句,我還強烈建議更改:

SOCKFILE=/html/public_html/yogavidya/run/gunicorn.sock

SOCKFILE=/var/run/yogavidya_gunicorn.sock

arch(以及其他一些發行版)上/run有一個 tmpfs,它只設定在記憶體中。從不存取檔案系統的 UNIX 套接字執行速度要快得多。

相關內容