0

I've got a Django app, which calls a program using subprocess.call(). This program creates a couple of files, which I then use back in my app. The problem is the program doesn't seem to have permission to create files. I also tried calls like subprocess.call(['mkdir','/tmp/myapp']) but the directory was not created. What do I need to do?

This is just with my dev server at the moment, which I invoke with

sudo python manage.py runserver 0.0.0.0:8080

I can run the command from the terminal manually fine.

I can also use subprocess to touch the files, which it does fine, but when it runs the program that accesses the files itself, that program throws an error because it cannot.

Eevee
  • 47,412
  • 11
  • 95
  • 127
fredley
  • 32,953
  • 42
  • 145
  • 236
  • 1
    Why are you running your server using sudo? – César Oct 16 '12 at 14:12
  • @César Because I did the first time (by mistake it seems), and since then starting without sudo caused problems (because it couldn't read it's own log files). Have now sorted, thanks! Sadly it doesn't solve this problem... – fredley Oct 16 '12 at 14:24
  • Maybe this [What permissions are required for subprocess.Popen?](http://stackoverflow.com/questions/2066068/what-permissions-are-required-for-subprocess-popen) or this [running a command as a super user from a python script](http://stackoverflow.com/questions/567542/running-a-command-as-a-super-user-from-a-python-script) can help – César Oct 16 '12 at 14:26
  • If you're just messing with the filesystem, have python to do it rather than spawning processes: http://docs.python.org/2/library/os.html#os.mkdir – John Mee Jul 19 '13 at 01:47

1 Answers1

1

The /tmp/myapp directory needs to be created first. If your view function in Django doesn't show you an error it means that you are not catching it properly. Try something like this:

response = HttpResponse()
calltxt = subprocess.call(['mkdir','/tmp/myapp'])
response.content = calltxt
return response

Append the function to your urls.py and load it in the browser. 0 means the command executed successfully, 1 means error (this is similar to the $! variable in Bash).

I think it's not the case but if your Django app is deployed in the Web Server the user that creates files and executes commands out of the Python environment is the one that owns the web server daemon. For instance www-data in Apache. Thus, it is needed to change permissions for this user also in order to write and execute commands out of their normal scope.

Juan
  • 1,022
  • 9
  • 16