test bash script execution from python
This commit is contained in:
parent
19843cc6eb
commit
344365d860
|
@ -27,7 +27,7 @@ def test():
|
|||
|
||||
@app.route("/phase1/")
|
||||
def phase1():
|
||||
stdout = check_output(['../scripts/traitements_phase_1.sh']).decode('utf-8')
|
||||
stdout = check_output(['/data/projets/ar_redadeg/scripts/traitements_phase_1.sh 2022']).decode('utf-8')
|
||||
return stdout
|
||||
|
||||
|
||||
|
|
34
api/test.py
Normal file
34
api/test.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
#!usr/local/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# https://stackoverflow.com/questions/53380988/how-to-execute-shell-script-from-flask-app
|
||||
|
||||
import subprocess
|
||||
from subprocess import Popen, PIPE
|
||||
from subprocess import check_output
|
||||
|
||||
script = '/data/projets/ar_redadeg/scripts/traitements_phase_1.sh 2022'
|
||||
|
||||
def get_shell_script_output_using_communicate():
|
||||
session = Popen([script], stdout=PIPE, stderr=PIPE, shell=True)
|
||||
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([script]).decode('utf-8')
|
||||
return stdout
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
return '<pre>'+get_shell_script_output_using_communicate()+'</pre>'
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# execute only if run as a script
|
||||
main()
|
||||
|
||||
|
Loading…
Reference in a new issue