How to execute Shell Script from Flask App [duplicate]












-1















This question already has an answer here:




  • Store output of subprocess.Popen call in a string

    9 answers




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?










share|improve this question















marked as duplicate by davidism flask
Users with the  flask badge can single-handedly close flask questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 1:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • What kind of error do you get? This looks similar: stackoverflow.com/questions/4256107/…, can you try the subprocess.Popen suggestion?
    – Seraf
    Nov 19 '18 at 19:04










  • Receive no error. I've tried subprocess.Popen and even os.system
    – jKraut
    Nov 19 '18 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
    – Seraf
    Nov 19 '18 at 19:15










  • @Seraf - sorry that was a typo, fixed it now
    – jKraut
    Nov 19 '18 at 19:19










  • Weird, the user that runs the Flask app is root? (Probably yes but still asking)
    – Seraf
    Nov 19 '18 at 19:25
















-1















This question already has an answer here:




  • Store output of subprocess.Popen call in a string

    9 answers




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?










share|improve this question















marked as duplicate by davidism flask
Users with the  flask badge can single-handedly close flask questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 1:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • What kind of error do you get? This looks similar: stackoverflow.com/questions/4256107/…, can you try the subprocess.Popen suggestion?
    – Seraf
    Nov 19 '18 at 19:04










  • Receive no error. I've tried subprocess.Popen and even os.system
    – jKraut
    Nov 19 '18 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
    – Seraf
    Nov 19 '18 at 19:15










  • @Seraf - sorry that was a typo, fixed it now
    – jKraut
    Nov 19 '18 at 19:19










  • Weird, the user that runs the Flask app is root? (Probably yes but still asking)
    – Seraf
    Nov 19 '18 at 19:25














-1












-1








-1


1






This question already has an answer here:




  • Store output of subprocess.Popen call in a string

    9 answers




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?










share|improve this question
















This question already has an answer here:




  • Store output of subprocess.Popen call in a string

    9 answers




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?





This question already has an answer here:




  • Store output of subprocess.Popen call in a string

    9 answers








python bash shell flask






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 19:19







jKraut

















asked Nov 19 '18 at 18:57









jKrautjKraut

57731019




57731019




marked as duplicate by davidism flask
Users with the  flask badge can single-handedly close flask questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 1:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by davidism flask
Users with the  flask badge can single-handedly close flask questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 1:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • What kind of error do you get? This looks similar: stackoverflow.com/questions/4256107/…, can you try the subprocess.Popen suggestion?
    – Seraf
    Nov 19 '18 at 19:04










  • Receive no error. I've tried subprocess.Popen and even os.system
    – jKraut
    Nov 19 '18 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
    – Seraf
    Nov 19 '18 at 19:15










  • @Seraf - sorry that was a typo, fixed it now
    – jKraut
    Nov 19 '18 at 19:19










  • Weird, the user that runs the Flask app is root? (Probably yes but still asking)
    – Seraf
    Nov 19 '18 at 19:25


















  • What kind of error do you get? This looks similar: stackoverflow.com/questions/4256107/…, can you try the subprocess.Popen suggestion?
    – Seraf
    Nov 19 '18 at 19:04










  • Receive no error. I've tried subprocess.Popen and even os.system
    – jKraut
    Nov 19 '18 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
    – Seraf
    Nov 19 '18 at 19:15










  • @Seraf - sorry that was a typo, fixed it now
    – jKraut
    Nov 19 '18 at 19:19










  • Weird, the user that runs the Flask app is root? (Probably yes but still asking)
    – Seraf
    Nov 19 '18 at 19:25
















What kind of error do you get? This looks similar: stackoverflow.com/questions/4256107/…, can you try the subprocess.Popen suggestion?
– Seraf
Nov 19 '18 at 19:04




What kind of error do you get? This looks similar: stackoverflow.com/questions/4256107/…, can you try the subprocess.Popen suggestion?
– Seraf
Nov 19 '18 at 19:04












Receive no error. I've tried subprocess.Popen and even os.system
– jKraut
Nov 19 '18 at 19:09




Receive no error. I've tried subprocess.Popen and even os.system
– jKraut
Nov 19 '18 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
– Seraf
Nov 19 '18 at 19:15




You gave permission to root for the file run_sql.sh but the file name you are trying to run is test.sh
– Seraf
Nov 19 '18 at 19:15












@Seraf - sorry that was a typo, fixed it now
– jKraut
Nov 19 '18 at 19:19




@Seraf - sorry that was a typo, fixed it now
– jKraut
Nov 19 '18 at 19:19












Weird, the user that runs the Flask app is root? (Probably yes but still asking)
– Seraf
Nov 19 '18 at 19:25




Weird, the user that runs the Flask app is root? (Probably yes but still asking)
– Seraf
Nov 19 '18 at 19:25












1 Answer
1






active

oldest

votes


















1














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 these 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 = subprocess.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






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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 these 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 = subprocess.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






    share|improve this answer


























      1














      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 these 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 = subprocess.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






      share|improve this answer
























        1












        1








        1






        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 these 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 = subprocess.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






        share|improve this answer












        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 these 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 = subprocess.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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 19:55









        arshoarsho

        3,28911531




        3,28911531















            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            ts Property 'filter' does not exist on type '{}'

            Notepad++ export/extract a list of installed plugins