FileNotFoundError:[Errno 2]没有这样的文件或目录:从.service文件运行gunicorn服务器时“bash”

smr*_*iti 3 python subprocess gunicorn

FileNotFoundError: [Errno 2] No such file or directory: 'bash'运行我的gunicorn python 应用程序表单.service 文件时出现错误。

但是,单独运行 Gunicorn 命令(而不是从 .service 文件)可以正常工作。

运行应用程序的gunicorn命令

gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 --bind <server_ip>:8080 wsgi
Run Code Online (Sandbox Code Playgroud)

应用程序.服务文件

[Service]
User=user
WorkingDirectory=/home/user/app
Environment="PATH=/home/user/app/app_venv/bin"
ExecStart=/home/user/app/app_venv/bin/gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker --workers 1 --bind <server_ip>:8080 wsgi
Run Code Online (Sandbox Code Playgroud)

生成错误的 Python 代码

import subprocess

cmd = ['bash', 'script.sh' , args.get('arg')]
try:
    process = subprocess.Popen(cmd,
                               cwd=/path/to/bash_script,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT,
                               universal_newlines=True)
    while process.poll() is None:
        output = process.stdout.readline()
        if(output==''):
            break
        emit('tg_output', output)

except subprocess.CalledProcessError as error:
    pass
Run Code Online (Sandbox Code Playgroud)

tri*_*eee 5

您明确设置

Environment="PATH=/home/user/app/app_venv/bin"
Run Code Online (Sandbox Code Playgroud)

您需要PATH包含要使用的任何外部二进制文件的所有目录(事实上,如果您无论如何都通过完整路径运行脚本,则实际上不需要包含脚本的目录;所以最好的解决方案可能只是PATH从文件中完全删除此分配)。

您的 Bash 脚本似乎不需要 Python 来运行它,并且您创建的用于运行它的 Python 包装器似乎有错误(特别是,毯子except看起来令人不安);也许更好的解决方案是完全运行一个单独的 Bash 进程。