nginx / unit

NGINX Unit - universal web app server - a lightweight and versatile open source server that simplifies the application stack by natively executing application code across eight different programming language runtimes.
https://unit.nginx.org
Apache License 2.0
5.25k stars 322 forks source link

Very hard to run .sh scripts from within Flask app #1193

Closed jli05 closed 3 months ago

jli05 commented 3 months ago

Hi,

My Flask app in the setup.py has lines

    setup(...,
               scripts=[...],
               ...)

so some .sh scripts are installed into venv/bin directory.

Then in the Flask app the scripts would be called by subprocess functions as Popen() etc.

Some script would call other scripts in the same venv/bin directory, and walk through filesystem somewhere. Overall it is very difficult to obtain expected results. I guess it is that the user is unit, different than my user.

I checked my nginx configuration file. It runs as user nginx but can run the Flask app with no problem, passing calls to the WSGI server uwsgi.

ac000 commented 3 months ago

Do you have working_directory0 set?

jli05 commented 3 months ago

You are right @ac000 . By default the environment variable PATH for Popen() is stringent even unitd is started with --user <my user>:

/bin:/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

So in the config.json I added working_directory parameter.

Then in Python code:

  from os import getcwd, environ
  from os.path import join
  env = {'PATH': join(getcwd(), 'venv', 'bin') + ':' + environ['PATH']}
  ...
  Popen([...], env=env, ...)