11

I've created and deployed a Flask App with Apache2 server WSGI in which now would like to run a .sh script from the App. However, from calling from python code, it doesn't execute.

Here is the test.sh:

#!/bin/bash echo "hi from shell script" 

Here is my python flask app code index.py (runs when App is opened) but nothing is printed or executed:

import subprocess subprocess.call('/var/www/FlaskApp/FlaskApp/scripts/test.sh') 

To check that there is not errors in my code, I've check flask error logs, and no errors. Also, I created a script called test_shell_script.py with same python code as above (but not flask app code) and it runs great like this:

# test_shell_script.py import subprocess subprocess.call('/var/www/FlaskApp/FlaskApp/scripts/test.sh') 

And then run it with python: python3 /var/www/FlaskApp/FlaskApp/test_shell_script.py

hi from shell script 

I did change the permissions as well:

-rwxr-xr-x 1 root root 364 Nov 19 17:48 ../scripts/test.sh 

What am I missing here which is not allowing my Flask app to run shell commands from the python code?

7
  • What kind of error do you get? This looks similar: stackoverflow.com/questions/4256107/…, can you try the subprocess.Popen suggestion? Commented Nov 19, 2018 at 19:04
  • Receive no error. I've tried subprocess.Popen and even os.system Commented Nov 19, 2018 at 19:09
  • You gave permission to root for the file run_sql.sh but the file name you are trying to run is test.sh Commented Nov 19, 2018 at 19:15
  • @Seraf - sorry that was a typo, fixed it now Commented Nov 19, 2018 at 19:19
  • Weird, the user that runs the Flask app is root? (Probably yes but still asking) Commented Nov 19, 2018 at 19:25

1 Answer 1

16

To show command output inside Python, there are two popular methods:

  1. check_output(): It runs command with arguments and return its output. (official documentation)
  2. subprocess.communicate(): Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. (official documentation)

I could view the shell file output using both methods using Python 3.5 in an Ubuntu machine.

app.py:

import subprocess from subprocess import Popen, PIPE from subprocess import check_output from flask import Flask def get_shell_script_output_using_communicate(): session = Popen(['./some.sh'], stdout=PIPE, stderr=PIPE) stdout, stderr = session.communicate() if stderr: raise Exception("Error "+str(stderr)) return stdout.decode('utf-8') def get_shell_script_output_using_check_output(): stdout = check_output(['./some.sh']).decode('utf-8') return stdout app = Flask(__name__) @app.route('/',methods=['GET',]) def home(): return '<pre>'+get_shell_script_output_using_check_output()+'</pre>' app.run(debug=True) 

some.sh:

#!/bin/bash echo "hi from shell script" echo "hello from shell script" 

Output screenshot:

enter image description here

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

1 Comment

In get_shell_script_output_using_communicate() stderr also needs to be decoded using raise Exception("Error "+str(stderr.decode('utf-8')))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.