Running multiple bash scripts simultaneously

In summary, the conversation discusses different ways to run two bash scripts simultaneously from a single terminal and potential pitfalls when using the subprocess module in Python. One solution is to have the python script write a bash script to run the scripts in parallel. Another solution is to use the subprocess.Popen function and wait for each process to finish before launching the next one. The conversation also mentions potential issues with scripts generating terminal output and how to avoid them.
  • #1
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
  • #2
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.
 
  • #3
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.
 
  • #4
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
  • #5
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.
 
  • #6
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
  • #7
Yes I know, I brought it up to make the OP be aware of what might happen when spawning processes that generate terminal output.
 

What does it mean to run multiple bash scripts simultaneously?

Running multiple bash scripts simultaneously means executing multiple bash scripts at the same time. This can be done by using a tool or command that allows for parallel processing, such as the "parallel" command or a job scheduler like "cron".

Why would someone want to run multiple bash scripts simultaneously?

There are several reasons why someone may want to run multiple bash scripts simultaneously. One reason is to save time by having multiple processes running concurrently. This can be useful for tasks that involve a lot of data processing or repetitive tasks. Another reason is to ensure that all tasks are completed in a timely manner, especially if there are dependencies between the scripts.

What are the potential challenges when running multiple bash scripts simultaneously?

One potential challenge is managing the resources of the system. Running multiple scripts simultaneously can use up a lot of CPU and memory, which can slow down other processes on the system. Another challenge is ensuring that the scripts are not conflicting with each other, especially if they are accessing the same files or resources.

How do I run multiple bash scripts simultaneously on a Linux system?

To run multiple bash scripts simultaneously on a Linux system, you can use the "parallel" command. This command allows you to specify which scripts to run and how many processes to use. You can also use a job scheduler like "cron" to schedule the execution of the scripts at specific times or intervals.

Are there any best practices for running multiple bash scripts simultaneously?

Yes, there are some best practices that can help ensure the smooth execution of multiple bash scripts simultaneously. These include optimizing the scripts for efficient resource usage, avoiding conflicts between scripts, and monitoring the system resources to prevent any issues. It is also recommended to test the scripts before running them simultaneously to identify any potential problems.

Similar threads

  • Programming and Computer Science
Replies
21
Views
2K
  • Programming and Computer Science
Replies
7
Views
463
  • Programming and Computer Science
Replies
2
Views
852
Replies
16
Views
2K
Replies
1
Views
2K
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
7
Views
4K
  • Computing and Technology
Replies
20
Views
2K
  • Programming and Computer Science
Replies
8
Views
792
Back
Top