无法使用Supervisord启动Python应用程序

无法使用Supervisord启动Python应用程序,python,linux,flask,daemon,supervisord,Python,Linux,Flask,Daemon,Supervisord,我有一个小应用程序,我想开始使用supervisord。我试过以下方法 请阅读中的简介、运行Supervisord和配置文件部分 通过将PID保存在文本文件中,我的初始shell脚本可以启动和停止芹菜和烧瓶作为守护进程。由于supervisord将负责杀死它,我去掉了停止部分,并将脚本非守护化 经过反复试验,我认为这些脚本和conf duo是有意义的,但它们不起作用 1. Shell脚本 #!/bin/bash if [[ $1 == "gunicorn" ]]

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

  • 请阅读中的简介、运行Supervisord和配置文件部分
通过将PID保存在文本文件中,我的初始shell脚本可以启动和停止芹菜和烧瓶作为守护进程。由于supervisord将负责杀死它,我去掉了停止部分,并将脚本非守护化

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

1. Shell脚本

#!/bin/bash


if [[ $1 == "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 [[ $1 == "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
#!/bin/bash
    
    
    if [[ $1 == "gunicorn" ]]
    then
        source env/bin/activate
        python env/bin/gunicorn -b 0.0.0.0:3333 -w 3 gunicornserve:app
    
    elif [[ $1 == "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=/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#u日志文件、自动启动、自动重新启动、stopasgroup、killasgroup与#1相同

我收到一条消息,说找不到命令源。我在同一个目录下的终端上尝试了相同的命令,它工作正常


3. Shell脚本

#!/bin/bash


if [[ $1 == "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 [[ $1 == "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
#!/bin/bash
    
    
    if [[ $1 == "gunicorn" ]]
    then
        source env/bin/activate
        python env/bin/gunicorn -b 0.0.0.0:3333 -w 3 gunicornserve:app
    
    elif [[ $1 == "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
形态

stderr#u日志文件、自动启动、自动重新启动、stopasgroup、killasgroup与#1相同


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

我出现以下错误,但我无法找出原因:-

不能产卵

进程退出太快

在#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给出了我在回答中使用的精彩建议。由于这里给出的解释,我找到了一个更简单的解决方案->

脚本文件

#!/bin/bash


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

elif [[ $1 == "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

在#2中,您不需要源代码。您可以使用:command=/path/to/env/bin/gunicorn-b0.0.0:3333-w2 gunicornserve:appNote表示#3。根据“如果是相对的,将搜索supervisord的环境$PATH以查找可执行文件”,这意味着
/hitler.sh
是从
$PATH
搜索的,而不是
/home/abhirath/Desktop/hitler
@ymonad,我不理解这一部分。我会请我的同事详细说明这一点。谢谢:)@MohammadAmin,但我需要激活Python环境。不是吗?我会试着给你回复:)@AbhirathMahipal这意味着你可能必须添加
environment=PATH=“/home/abhirath/Desktop/Hitler:/usr/local/bin:/usr/bin:/bin”
如果你想设置
command=/Hitler.sh,我就试试看。我能等一段时间再接受你的答案吗?当然,但请为你的问题选择一个答案,这样将来的访问者也可以使用它。谢谢。绝对:)我退出退出
太快了
。它给出了无法生成进程,并且像
无法找到module
gunicorn和芹菜这样的语句。我还用
python/path/to/env/bin/gunicorn…
进行了尝试。先别自寻烦恼了,我会先试试@ymonad的解决方案。我认为答案之间没有太大区别。“我喜欢用一种类似蟒蛇的方式来做这件事。”MohammadAmin实际上是的。我必须键入相同次数的路径。再次接受你的回答。