Running multiple bash scripts simultaneously

  • Context: Python 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Multiple Running
Click For Summary

Discussion Overview

The discussion revolves around methods to run multiple bash scripts simultaneously from a Python script, focusing on subprocess management and terminal interaction. Participants explore various approaches and potential issues related to output handling.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant questions whether using subprocess.call will execute run1.sh and then run2.sh sequentially, suggesting that this approach does not allow for simultaneous execution.
  • Another participant confirms that subprocess.call will indeed run the scripts sequentially and suggests using subprocess.Popen for non-blocking execution.
  • A different approach is proposed where a Python script generates a bash script that runs multiple scripts in parallel using "&" to allow for simultaneous execution.
  • One participant describes a method using subprocess.Popen to run scripts sequentially, waiting for each to finish before starting the next.
  • Concerns are raised about issues with scripts that generate output, leading to potential freezing when reading back the output.
  • Another participant reiterates the output handling issue, referencing the Python documentation on subprocess for potential pitfalls.
  • One participant acknowledges the output issue to ensure the original poster is aware of the complications that may arise when spawning processes that generate terminal output.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method for running scripts simultaneously, with multiple competing views and approaches presented throughout the discussion.

Contextual Notes

Participants express concerns about the limitations of subprocess management, particularly regarding output handling and terminal interaction, which remain unresolved.

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   Reactions: 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   Reactions: 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
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
Replies
16
Views
4K
  • · 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