3

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
smriti
  • 1,073
  • 1
  • 13
  • 25

2 Answers2

5

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
-1

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.

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • This is not bad advice as such; but the problem is they just killed their `PATH` completely, so basically _nothing_ will work without a full path. – tripleee Jun 20 '23 at 05:16