I want to write a bash script where I run two commands simultaneously, then continue when they both complete.
Here's something that doesn't work, but I'll put it here to illustrate what I'm trying to do:
#!/bin/bash ./job1 & ./job2 ./dostuffwithresults
The script will run both job1 and job2 at the same time, but will only wait for job2 to finish before continuing. If job1 takes longer, then the results might not be ready for the final command.
The bash wait command is a Shell command that waits for background running processes to complete and returns the exit status. Unlike the sleep command, which waits for a specified time, the wait command waits for all or specific background tasks to finish.
How to Use the Bash Sleep Command. Sleep is a very versatile command with a very simple syntax. It is as easy as typing sleep N . This will pause your script for N seconds, with N being either a positive integer or a floating point number.
GNU parallel examples to run command or code in parallel in bash shell. From the GNU project site: GNU parallel is a shell tool for executing jobs in parallel using one or more computers. A job can be a single command or a small script that has to be run for each of the lines in the input.
j1 & j2 & j3 & wait $(jobs -p) dostuffwithresults
something like this should work
#!/bin/bash while [ `pgrep job*` ] do echo 'waiting' done ./dostuffwithresults
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With