Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a bash script from Python

Tags:

python

linux

bash

I need to run a bash script from Python. I got it to work as follows:

import os
os.system("xterm -hold -e scipt.sh")

That isn't exactly what I am doing but pretty much the idea. That works fine, a new terminal window opens and I hold it for debugging purposes, but my problem is I need the python script to keep running even if that isn't finished. Any way I can do this?

like image 789
ajk4550 Avatar asked Feb 14 '23 11:02

ajk4550


1 Answers

I recommend you use subprocess module: docs

And you can

import subprocess

cmd = "xterm -hold -e scipt.sh"
# no block, it start a sub process.
p = subprocess.Popen(cmd , shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# and you can block util the cmd execute finish
p.wait()
# or stdout, stderr = p.communicate()

For more info, read the docs,:).

edited misspellings

like image 146
atupal Avatar answered Feb 17 '23 01:02

atupal