0

stack overflow.

I'm trying to run some simple bash script (.sh) from Django view.

My views.py:

def start_stop(request):
    subprocess.Popen(['/home/ubuntu/contacts/contacts/scripts/test.sh'], shell=True)
    result = 'Success'
    return HttpResponse(result)

My bash script test.sh:

echo "testing text"
source /home/ubuntu/contacts/ENV/bin/activate
cd /home/ubuntu/contacts/contacts/scripts/
python final_poster.py

My main goal is activate the virtualenv and start python script name "final_poster.py". But it doesn't work. I also tried to use subprocess.call without any success result. I am stuck on it, can you help me?

John Smith
  • 117
  • 1
  • 12

1 Answers1

0

I think if don't see any error on the shell, then the script is running properly. If you want to see python shell's output, then you can add logging to bash script. For example:

echo "testing text"
source /home/ubuntu/contacts/ENV/bin/activate
cd /home/ubuntu/contacts/contacts/scripts/
python final_poster.py >> logfile.log

Check this SO answer for details.

Community
  • 1
  • 1
ruddra
  • 50,746
  • 7
  • 78
  • 101
  • I tried to do something similar with the `.sh` file having shebang `#!/bin/sh` which didn't work but `#!/bin/bash` did work. – curtisp Jul 24 '20 at 16:52