简体   繁体   中英

FileNotFoundError: [Errno 2] No such file or directory: 'bash' when running gunicorn server from .service file

Getting FileNotFoundError: [Errno 2] No such file or directory: 'bash' error while running my gunicorn python app form .service file.

However running gunicorn command by itself(not from .service file) works fine.

gunicorn command to run the app

gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 --bind <server_ip>:8080 wsgi

app.service file

[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

Python code that is generating the error

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

You are explicitly setting

Environment="PATH=/home/user/app/app_venv/bin"

You need for PATH to contain all the directories of any external binaries you want to use (and in fact, there is no need really for it to contain the directory of your script, if you are running it by full path anyway; so the best solution is probably simply to remove this PATH assignment from the file altogether).

Your Bash script doesn't seem to need Python to run it, and the Python wrapper you created to run it seems to have bugs (in particular, the blanket except looks unnerving); perhaps a better solution would be to run a separate Bash process altogether.

IMO the bash command is not in the user PATH. It's better to always use the full path of the bash command.

cmd = ['/bin/bash', 'script.sh' , args.get('arg')]

Use which bash to get the full path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM