Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell scripting trying to launch tasks in parallel?

The basic task I need to do is call a python script multiple times, each time with a different argument. For example:

script.py -t command1
script.py -t command2
script.py -t command3

Etc. Now the problem is that script.py takes a long time to run, so just putting the above in a shell script and running them in series (one after another) is a waste of time.

Instead what I'd like to be able to do is open up multiple terminals, and in each one, run

script.py -t command_i

For the ith command argument. Is there any way of doing this? If not, are there any other helpful parallel options for what I'm trying to do?

Cheers

EDIT - just realized: I probably want some sort of forking, yes? I've never done that with shell scripting though.

like image 869
JDS Avatar asked Jun 07 '26 08:06

JDS


1 Answers

To run a command in the background, add an & at the end:

script.py -t command1 &
script.py -t command2 &
script.py -t command3 &

If you are using this in a shell script and want to wait for all the processes to finish, run wait without arguments. It will wait for all background jobs in the current shell to complete before continuing the script.

like image 106
Emil Vikström Avatar answered Jun 10 '26 05:06

Emil Vikströ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!