私はこの質問が以前に尋ねられたことを理解していますが、アドバイスされた方法は私にはうまくいきません。これが私のDjangoビューからやりたいことです:
sudo python mypythonscript.py arg1 arg2 > mypythonscript.log
これをコマンドラインから実行すると、チャームのように機能しますが、djangoビューでは機能しません。os.system(command)とsubprocess.call(command、shell = True)を使用してみましたが、機能しません。前もって感謝します。
編集:これは私の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")
全体的な話は、自分のWebサイトにフォームがあり、そのフォームの内容を引数としてlaunch_instances.pyスクリプトに渡したいということです。フォームの送信ボタンを押すと、/ luanch_instances /に投稿され、このビューに「リダイレクト」されます。コードをそのまま実行しても何も起こりません。新しいページに「作成中の何かが後でここに表示されます」と表示されるだけです。私が今までに使用した場合
suprocess.check_call(command, shell=True)
これは私が得るものです:
Command 'sudo python /home/bitnami/launch_instances.py 1 ami-ff02058b > /home/bitnami/launchinstances.log' returned non-zero exit status 2