Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute Shell Script from Flask App [duplicate]

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?

like image 664
jKraut Avatar asked Nov 19 '18 18:11

jKraut


People also ask

How do you run a flask in shell script?

Give the configuration a name such as “flask run”. Click the Script path dropdown and change it to Module name, then input flask . The Parameters field is set to the CLI command to execute along with any arguments. This example uses --app hello --debug run , which will run the development server in debug mode.

How do I run flask app from terminal?

To run the app outside of the VS Code debugger, use the following steps from a terminal: Set an environment variable for FLASK_APP . On Linux and macOS, use export set FLASK_APP=webapp ; on Windows use set FLASK_APP=webapp . Navigate into the hello_app folder, then launch the program using python -m flask run .

How do you access a flask shell?

Flask itself does not come with an interactive shell, because it does not require any specific setup upfront, just import your application and start playing around.


1 Answers

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

like image 80
arsho Avatar answered Nov 15 '22 05:11

arsho