无法使用 Supervisord 启动 Python 应用

Unable to start a Python app using Supervisord

我有一个小应用程序,我想开始使用 supervisord。我试过以下方法

我最初的 shell 脚本可以通过将 PID 保存在文本文件中来启动和停止 celery 和 Flask 作为守护进程。因为 supervisord 会负责杀死它,所以我去掉了停止部分并且没有守护脚本。

经过反复试验后,我认为这些脚本和 conf 组合是有意义的,但它们不起作用。

1

Shell 脚本

#!/bin/bash


if [[  == "gunicorn" ]]
then
    cd /home/abhirath/Desktop/Hitler
    source env/bin/activate
    python env/bin/gunicorn -b 0.0.0.0:3333 -w 3 gunicornserve:app

elif [[  == "celery" ]]
then
    cd /home/abhirath/Desktop/Hitler
    source env/bin/activate
    python env/bin/celery -A testrunner worker --concurrency=3 --loglevel=info

else
    echo "Usage:-"
    echo "To start celery:-"
    echo "./hitler.sh celery"
    echo "To start Gunicorn"
    echo "./hitler.sh gunicorn"
fi

配置文件

[group:hitler]
programs=gunicorn,celery

[program:gunicorn]
command=/home/abhirath/Desktop/Hitler/hitler.sh gunicorn
stderr_logfile =/home/abhirath/Desktop/supervisor.err.log
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true

[program:celery]
command=/home/abhirath/Desktop/Hitler/hitler.sh celery
stderr_logfile=/home/abhirath/Desktop/supervisor.err2.log
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true

2

没有 shell 文件

[group:hitler]
programs=gunicorn,celery

[program:gunicorn]
command=source env/bin/activate; python env/bin/gunicorn -b 0.0.0.0:3333 -w 2 gunicornserve:app;
directory=/home/abhirath/Desktop/Hitler

[program:celery]
command=source env/bin/activate; python env/bin/celery -A testrunner worker --concurrency=4 --loglevel=info;
directory=/home/abhirath/Desktop/Hitler

stderr_logfile、自动启动、自动重启、stopasgroup、killasgroup 与 #1

相同

我收到一条消息说找不到命令 source。我在同一目录中的终端上尝试了相同的命令并且它有效。


3

Shell 脚本

#!/bin/bash
    
    
    if [[  == "gunicorn" ]]
    then
        source env/bin/activate
        python env/bin/gunicorn -b 0.0.0.0:3333 -w 3 gunicornserve:app
    
    elif [[  == "celery" ]]
    then
        source env/bin/activate
        python env/bin/celery -A testrunner worker --concurrency=3 --loglevel=info
    
    else
        echo "Usage:-"
        echo "To start celery:-"
        echo "./hitler.sh celery"
        echo "To start Gunicorn"
        echo "./hitler.sh gunicorn"
    fi

会议

[group:hitler]
programs=gunicorn,celery

[program:gunicorn]
command=./hitler.sh gunicorn
directory=/home/abhirath/Desktop/Hitler    

[program:celery]
command=./hitler.sh celery
directory=/home/abhirath/Desktop/Hitler

stderr_logfile、自动启动、自动重启、stopasgroup、killasgroup 与 #1

相同

我也尝试使用 command=bash -c "command here",尽管我觉得在上述所有情况下都不需要它。文档中提到了 here

我收到以下错误,但我无法弄清楚原因:-

Could not spawn

Process Exited too quickly

在#2 的情况下,您实际上不需要激活 virtualenv。您可以将其更改为:

[group:hitler]
programs=gunicorn,celery

[program:gunicorn]
command=/absolute/path/to/env/bin/gunicorn /absolute/path/to/gunicornserve:app -b 0.0.0.0:3333 -w 2
directory=/home/abhirath/Desktop/Hitler

[program:celery]
command=/absolute/path/to/env/bin/celery -A testrunner worker --concurrency=4 --loglevel=info
directory=/home/abhirath/Desktop/Hitler

@MohammadAmin 和@ymonad 给出了很好的建议,我在这个答案中使用了这些建议。由于此处给出的解释,我找到了一个更简单的解决方案 -> Supervising virtualenv django app via supervisor

脚本文件

#!/bin/bash


if [[  == "gunicorn" ]]
then
  env/bin/python env/bin/gunicorn -b 0.0.0.0:3333 -w 3 gunicornserve:app 

elif [[  == "celery" ]]
then
  export C_FORCE_ROOT='true'
  env/bin/python env/bin/celery -A testrunner worker --concurrency=3 --loglevel=info

else
  echo "Usage:-"
  echo "To start celery:-"
  echo "./hitler.sh celery"
  echo "To start Gunicorn"
  echo "./hitler.sh gunicorn"

fi

由于我指定项目目录作为环境路径,所以我不需要在环境中指定Python解释器的绝对路径。

主管会议

[group:hitler]
programs=gunicorn,celery

[program:gunicorn]
command=bash -c "./hitler.sh gunicorn" 
directory=/home/abhirath/Desktop/Hitler
environment=PATH="/home/abhirath/Desktop/Hitler"
stderr_logfile=/home/abhirath/Desktop/Hitler/Logs/gunicornerr.log
stderr_logfile_backups=1
stdout_logfile=/home/abhirath/Desktop/Hitler/Logs/gunicornout.log
stdout_logfile_backups=1
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
startretries=0

[program:celery]
command=bash -c "./hitler.sh celery"
directory=/home/abhirath/Desktop/Hitler
environment=PATH="/home/abhirath/Desktop/Hitler"
stderr_logfile=/home/abhirath/Desktop/Hitler/Logs/celeryerr.log
stderr_logfile_backups=1
stdout_logfile=/home/abhirath/Desktop/Hitler/Logs/celeryout.log
stdout_logfile_backups=1
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
startretries=0