Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute bash commands in the same process in python

I need to run a large build script (bash commands) on a python script. I receive it as a large string and each line is splitted by a \n. So, I need to execute each line separately.

At first, I tried to use subprocess.Popen() to execute them. But the problem is: after each line, the process terminates and all the environment variables are lost.

The problem is not to wait a command to finish to execute another, I need all of them to be executed on the same shell.

The only solution that I found so far is to save all those commands as a sh file (for example build.sh) and execute it on python.

I would not like to use this approuch because I want to have more control over each execution.

Is there any way to execute those commands on the same process, one by one?

Any other solution would be nice too.

like image 852
leandrocastelli Avatar asked May 22 '26 18:05

leandrocastelli


1 Answers

What you want is definitely a little weird, but it's possible using pipes.

from subprocess import PIPE, Popen

p = Popen(['bash'], stdin=PIPE, stdout=PIPE)
p.stdin.write('echo hello world\n')
print(p.stdout.readline())
# Check a return code
p.stdin.write('echo $?\n')
if p.stdout.readline().strip() ⩵ '0':
    print("Command succeeded")
p.stdin.write('echo bye world\n')
# Close input and wait for bash to exit
stdout, stderr = p.communicate()
print(stdout)
like image 144
Keith M Avatar answered May 27 '26 18:05

Keith M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!