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
3
  • 3
    You need to tell us why didn't it work. Show your code and output. Commented Aug 1, 2012 at 8: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. Commented Aug 1, 2012 at 8:32
  • @VladimirVolodin i've added the output and code now Commented Aug 1, 2012 at 8:48

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
But remember that doing chmod a+rx may break security.
its alright i didn't need to use chmod a+rx in the end. Simply importing the script did the job :) cheers
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)

1 Comment

assuming that by input=pipe you meant input=subprocess.PIPE, I get the followng error: 'int' object has no attribute 'getitem'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.