insumity
insumity

Reputation: 5459

Execute multiple processes using bash

I want to run multiple processes in parallel using bash. I did the following:

./proc sth1 & ./proc sth2 & ./proc sth3 & ... & ./proc sthN &

The problem with the above is that it immediately ends. So if I do: time (./proc sth1 & ... & ./proc sthN &) I get back 0.

I want to run the above command but I want it to stop when the last process has finished. So if ./proc sthX takes 10 seconds while all the other processes take 1 second. I want to wait for 10 seconds till the above command returns. Is there a way to do this?

Upvotes: 2

Views: 3414

Answers (4)

Kostia R
Kostia R

Reputation: 2565

While "wait and & combination" might be sufficient, I found that it's not what I want most of the time. What I want is that return-code would indicate failure in case any command fails by default. So I created a small utility called par:

https://github.com/k-bx/par

Upvotes: 0

Idriss Neumann
Idriss Neumann

Reputation: 3838

Another example to use wait with a list of jobs :

nbf=0
jobs -p|while read -r; do
    wait $REPLY || (( nbf++ ))
done
echo "$nbf jobs ended with failure" >&2

Upvotes: 0

Gilles Quénot
Gilles Quénot

Reputation: 184965

wait is designed for this :

./proc sth1 & ./proc sth2 & ./proc sth3 & ... & ./proc sthN &
 wait

Some doc :

$ LANG=C help wait
wait: wait [id]
    Wait for job completion and return exit status.

    Waits for the process identified by ID, which may be a process ID or a
    job specification, and reports its termination status.  If ID is not
    given, waits for all currently active child processes, and the return
    status is zero.  If ID is a a job specification, waits for all processes
    in the job's pipeline.

    Exit Status:
    Returns the

status of ID; fails if ID is invalid or an invalid option is given.

Another solution is to get all pids and put a wait for all of these pids.

Upvotes: 1

Petr
Petr

Reputation: 63349

Just call wait at the end. Quoting bash manual Job control builtins:

wait [jobspec or pid ...]

Wait until the child process specified by each process ID pid or job specification jobspec exits and return the exit status of the last command waited for. If a job spec is given, all processes in the job are waited for. If no arguments are given, all currently active child processes are waited for, and the return status is zero. If neither jobspec nor pid specifies an active child process of the shell, the return status is 127.

An example:

#!/bin/bash
function test {
    time=$(( RANDOM % 10 ))
    echo "Sleeping for $time"
    sleep "$time"
    echo "Slept for $time"
}

time (
    test & test & test & test & test & test &
    wait
    echo "Finished all."
)

Upvotes: 6

Related Questions