Python Running multiple bash scripts simultaneously

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Multiple Running
Click For Summary
To run two bash scripts, run1.sh and run2.sh, simultaneously from a single terminal, the subprocess module in Python can be utilized, specifically the subprocess.Popen function. Unlike subprocess.call, which waits for a script to finish before executing the next, Popen allows for concurrent execution. However, it's important to note that Popen does not provide interactive terminal capabilities for each subprocess, meaning that if interaction is required, separate terminal windows must be opened for each script. An alternative approach is to create a launcher bash script that runs the scripts in parallel using the "&" operator, though this method sacrifices some control over tracking the scripts' outputs. Additionally, issues may arise when scripts generate output, potentially causing them to freeze if not handled properly. The Python documentation on the subprocess module outlines these challenges and offers guidance on managing subprocesses effectively.
member 428835
Hi PF!

How can I run two bash scripts run1.sh run2.sh simultaneously (having two separate terminals, one for each run.sh, is fine but I'd like to execute the python script from a single terminal)? Won't the below execute run1.sh and then when it's finished execute run2.sh?

Python:
subprocess.call(['./run1.sh'])
subprocess.call(['./run2.sh'])
 
Technology news on Phys.org
joshmccraney said:
Won't the below execute run1.sh and then when it's finished execute run2.sh?

Yes. However, there are other ways to run a subprocess in Python that don't wait for the process to complete. Look at the documentation for subprocess.Popen.

Note, however, that none of the subprocess module functions in Python will give the subprocess a terminal you can interact with like the normal shell terminal you are used to. (There are ways to do this in a limited fashion using Popen.communicate, but it's pretty complicated.) So if by "each one has a separate terminal", you mean that you need to interact with each one separately in a terminal window, you have to actually open two terminal windows and run each script in one of them. You can't do it by running a single Python script.
 
You could also have your python script write a bash script of scripts to run in parallel using "&" ending characters and then run it.

Bash:
#!/bin/bash
echo "start of launcher script"

run_script_a &
run_script_b &
run_script_c &

echo "end of launcher script"

admittedly you lose the control of tracking the three scripts but its overall a simpler solution than having your python run them without blocking.
 
I typically do it like this. This will run run1, and wait for it to finish before launching run2.
Python:
step1 = subprocess.Popen('./run1.sh',shell=True)
subprocess.Popen.wait(step1)
step2 =subprocess.Popen('./run2.sh',shell=True)
subprocess.Popen.wait(step2)
 
  • Like
  • Informative
Likes Klystron and jedishrfu
I've had issues when scripts generate output and you are reading it back. Basically the scripts would appear to freeze. This occurred with an awk script that launched commands and scanned their output.
 
jedishrfu said:
I've had issues when scripts generate output and you are reading it back. Basically the scripts would appear to freeze.

The Python documentation on the subprocess module discusses a number of potential pitfalls when doing this and how to avoid them.
 
  • Like
Likes jedishrfu
Yes I know, I brought it up to make the OP be aware of what might happen when spawning processes that generate terminal output.
 

Similar threads

  • · Replies 21 ·
Replies
21
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
16
Views
3K
  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K