python - Supervisor - 启动 django 应用程序并通过 bash 文件保持其运行

标签 python django bash supervisord

我基本上是在尝试按照标题所说的那样 - 保持 django 应用程序运行,即使在服务器重新启动时也是如此。

我遵循了一些教程,但没有成功让supervisord为我处理这个任务。

我目前有一个confMyConf.conf/etc/supervisor/conf.d ,看起来像:

[program:MyConf]
command = /home/user/virtualenv/gunicorn_start.bash                                                         ; Command to start app
autostart=true                                                                                          ; start app when system starts
autorestart=true                                                                                        ; defines how app starts in event app exits
user=vanew                                                                                              ; User to run as
stdout_logfile=/home/user/virtualenv/nginxConfiguration/error/gunicorn_supervisor.log                  ; Where to write log messages
stderr_logfile=/home/user/virtualenv/nginxConfiguration/error/gunicorn_supervisor_error.log            ; Where to write log error messages
redirect_stderr = true  

gunicorn_start.bash

   #!/bin/bash
    NAME="myapp"                                            # Name of the application
    DJANGODIR=/home/useros/virtualenv/MyConf/myapp              # Django project directory
    #SOCKFILE=/webapps/hello_django/run/gunicorn.sock           # we will communicte using this unix socket
    USER=user                                                   # the user to run as
    GROUP=user                                                  # the group to run as
    NUM_WORKERS=5                                               # how many worker processes should Gunicorn
    DJANGO_SETTINGS_MODULE=MyConf.settings                      # which settings file should Django use
    DJANGO_WSGI_MODULE=MyConf.wsgi                              # WSGI module name

    echo "Starting $NAME"

    # Activate the virtual environment
    cd $DJANGODIR
    source ../../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 /usr/local/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
    --name $NAME \
    --workers $NUM_WORKERS \
    --user=$USER --group=$GROUP \
    --log-level=debug \
    --bind=unix:$SOCKFILE

它一直在 stdout_logfile 中给我以下错误文件,当我运行以下命令时:sudo supervisorctl start MyConf :

Starting myapp
dirname: missing operand
Try 'dirname --help' for more information.
2014-06-14 01:20:15 [3698] [INFO] Starting gunicorn 18.0
Traceback (most recent call last):
  File "/usr/local/bin/gunicorn", line 9, in <module>
    load_entry_point('gunicorn==18.0', 'console_scripts', 'gunicorn')()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 71, in run
    WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/base.py", line 143, in run
    Arbiter(self).run()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 172, in run
    self.start()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 134, in start
    self.LISTENERS = create_sockets(self.cfg, self.log)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/sock.py", line 198, in create_sockets
    sock = sock_type(addr, conf, log)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/sock.py", line 102, in __init__
    super(UnixSocket, self).__init__(addr, conf, log, fd=fd)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/sock.py", line 31, in __init__
    self.sock = self.set_options(sock, bound=(fd is not None))
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/sock.py", line 42, in set_options
    self.bind(sock)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/sock.py", line 110, in bind
    util.chown(self.cfg_addr, self.conf.uid, self.conf.gid)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 157, in chown
    os.chown(path, uid, gid)
OSError: [Errno 2] No such file or directory: ''

注:gunicorn_start.bash已通过 chmod +x gunicorn_start.bash 可执行

显然它无法找到文件或目录,是因为什么吗?我该如何解决这个问题?

最佳答案

为什么不只使用主管配置来设置所有内容?在您的主管conf中似乎缺少 directory 的值:

[program:gunicorn]
directory=/home/useros/virtualenv/MyConf
command=/home/useros/virtualenvs/MyConf/bin/python manage.py run_gunicorn -b unix:/tmp/gunicorn.sock

numprocs=4

user=vanew
autostart=true
autorestart=true

process_name=%(program_name)s_%(process_num)s
stdout_logfile=/var/log/supervisor/%(program_name)s_%(process_num)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s_%(process_num)s.err

关于python - Supervisor - 启动 django 应用程序并通过 bash 文件保持其运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24215789/

相关文章:

Django 动态更改表单上所需的属性

django - django 的 ORM 线程安全吗?

bash - 使用 Bash 脚本在子文件夹中执行 ImageMagick 转换

python - 如何从 cookiejar 中形成 Cookie header ?

python - 并发访问同一个.pyc

python - 通过 L0 范数/GEKKO 中非零元素的数量约束混合整数非线性优化问题

linux - 如何在 Linux 中使用 curl 或任何 shell 脚本或任何命令行实用程序将整个目录上传到 artifactory?

python - 如何根据两列的值删除重复行?

django - 使用 @user_passes_test 装饰器测试重定向

linux - 命令末尾的 "</dev/null >&/dev/null"有什么作用?