3

I understand this question has been asked before but the methods adviced don't work for me. This is what I want to do from my Django view:

sudo python mypythonscript.py arg1 arg2 > mypythonscript.log

If I execute this from command line it works like a charm but can't get it to work through django views. I have tried using os.system(command) and subprocess.call(command, shell=True) but they don't work. Thanks in advance.

EDIT: This is my views.py:

from django.http import HttpResponse
import datetime
import subprocess

def li_view(request):
if request.is_ajax():
        if request.method == 'GET':
            message = "This is an XHR GET request"
        elif request.method == 'POST':
            message = "This is an XHR POST request"
            print request.POST
    else:
        message = "No XHR"
    num_instances = request.POST['num_instances']
    ami_id = "ami-ff02058b"
    command = "sudo python /home/bitnami/launch_instances.py 1 " + num_instances + " " + ami_id + " > /home/bitnami/launcinstances.log"
    subprocess.call(commad, shell=True)
    return HttpResponse("something creative will go here later")

The whole story is that I have a form on my website and I want to pass the contents of that form as arguements to my launch_instances.py script. When I press the submit button in my form it posts to /luanch_instances/ which 'redirects' to this view. Executing the code as it is will do nothing, it'll simply just show me "something creating will go here later" on a new page. If I was to how ever use

suprocess.check_call(command, shell=True)

This is what I get:

Command 'sudo python /home/bitnami/launch_instances.py 1 ami-ff02058b > /home/bitnami/launchinstances.log' returned non-zero exit status 2
AmirHBP
  • 267
  • 1
  • 4
  • 13
  • 3
    You need to tell us why didn't it work. Show your code and output. – devmiles.com Aug 01 '12 at 08:26
  • 2
    Also what are you trying to accomplish by doing this in a python view? It is generally advisable to use a task queue such as Celery to perform this sorts of things as it doesn't tie up your web server. – Rory Hart Aug 01 '12 at 08:32
  • @VladimirVolodin i've added the output and code now – AmirHBP Aug 01 '12 at 08:48

2 Answers2

3

As you're trying to run python script, you may simply import code from launch_instances.py into your view, redirect output to /home/bitnami/launcinstances.log (about redirecting stdout to file in python: Redirect stdout to a file in Python?). But there's still problem with root privileges - one option is to change permissions of: resources needed for calling code from launch_instances.py; your log file; to allow your django process to execute that, second option (not recommended) is to run django app as root.

Community
  • 1
  • 1
Marek
  • 439
  • 2
  • 5
  • 12
  • I don't know why executing: sudo python mypythonscript.py arg1 arg2 > mypythonscript.log needs root permissions. Assume that mypythonscript.py is not owned by owner of django process, then you may simply do chmod a+rx mypythonscript.pl – Marek Aug 01 '12 at 09:26
  • But remember that doing chmod a+rx may break security. – Marek Aug 01 '12 at 10:45
  • its alright i didn't need to use chmod a+rx in the end. Simply importing the script did the job :) cheers – AmirHBP Aug 01 '12 at 12:08
3

Most likely it has something to do with permissions. Try examining stderr and stdout of your command execution. I'm using this code to debug my shell commands:

process = subprocess.Popen(shlex.split(command), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output,error = process.communicate(input=pipe)
devmiles.com
  • 9,895
  • 5
  • 31
  • 47
  • assuming that by input=pipe you meant input=subprocess.PIPE, I get the followng error: 'int' object has no attribute '__getitem__' – AmirHBP Aug 01 '12 at 09:11