0

Application Details: Ubuntu 16.04 + flask + nginx + uwsgi

I am trying to execute a bash command from flask application.

@app.route('/hello', methods=('GET', 'POST'))
def hello():
    os.system('mkdir my_directory')
    return "Hello"

The above code run successfully but doesn't create any directory. Also it creates directory on my local which doesn't have any nginx level setup.

I also tried following ways:

  1. subprocess.call(['mkdir', 'my_directory']) # Throws Internal server error
  2. subprocess.call(['mkdir', 'my_directory'],shell=True) # No error but directory not created
  3. subprocess.Popen(['mkdir', 'my_directory']) # Throws Internal server error
  4. subprocess.Popen(['mkdir', 'my_directory'],shell=True) # No error but directory not created

Do I need any nginx level configuration changes.

Leon
  • 2,926
  • 1
  • 25
  • 34
mdaftab88
  • 291
  • 3
  • 8
  • 2
    Did you try `import os os.makedirs(my_directory)` ? – Pradeepb Aug 24 '18 at 10:33
  • 1
    it will create my_directory inside whatever directory your app is running on, try giving it an absolute path instead of a directory name. e.g. mkdir /home/you/output_dirs/my_directory – E.Serra Aug 24 '18 at 10:36
  • The code attempts to create the directory in the working directory. Try to see the working directory by os.getcwd(). May the application doesn't have r/w access to that folder. Also, check the return code for subprocess.call. – apatniv Aug 24 '18 at 10:37
  • @Pradeepb os.makedirs is working. But any shell command is not working. – mdaftab88 Aug 24 '18 at 10:53
  • @VivekAkupatni subprocess.call is returning 127. And I am creating directory in the current working directory which I think all r/w access. Because I am able to create directory using `os.makedirs` from the same point of execution. – mdaftab88 Aug 24 '18 at 11:07
  • The ones which give "Internal server error" are correct. The problem is *probably* that you don't have permission to create a directory in that location. (Well, except you should use `check_call()` instead of `call()` and definitely not bare `Popen()` here. See also https://stackoverflow.com/questions/4256107/running-bash-commands-in-python/51950538#51950538) – tripleee Aug 24 '18 at 11:12
  • When I am executing `subprocess.Popen("mkdir my_directory",shell=True, executable='/bin/bash')` then in my syslog, I am getting `/bin/bash: mkdir: command not found` – mdaftab88 Aug 24 '18 at 12:58

1 Answers1

2

Finally I got the point. I followed Python subprocess call returns “command not found”, Terminal executes correctly .

What I was missing was absolute path of mkdir. When I executed subprocess.call(["/bin/mkdir", "my_directory"]), it makes the directory successfully.

The above link contains complete details.

Also I would be thankful if anyone will describe the reason that why I need to specify absolute path for mkdir.

Thanks to all. :)

Community
  • 1
  • 1
mdaftab88
  • 291
  • 3
  • 8
  • also i have the same problem, i think it is from uwsgi, because when i run the flask development server i don't use the full path, i use just "mkdir". – anasmorahhib Sep 03 '20 at 15:47