views:

286

answers:

4

Is there a simple way to do the equivalent of this, but run the two processes concurrently with bash?

$ time sleep 5; sleep 8

time should report a total of 8 seconds (or the amount of time of the longest task)

+1  A: 
time sleep 8 & time sleep 5

The & operator causes the first command to run in the background, which practically means that the two commands will run concurrently.

Ayman Hourieh
Oh yes, didn't think of that third interpretion of his question. Good one.
TheBonsai
+1  A: 

Using sleeps as examples.

If you want to only time the first process, then

time sleep 10 & sleep 20

If you want to time both processes, then

time (sleep 10 & sleep 20)
TheBonsai
The second example doesn't correctly time both processes, just the second.
Grant
You're right, but regarding his newer comments, it's more or less what he asked for (his initial question wasn't clear enough).
TheBonsai
A: 

Sorry my question may not have been exactly clear the first time around, but I think I've found an answer, thanks to some direction given here.

time sleep 5& time sleep 8

will time both processes while they run concurrently, then I'll just take the larger result.

Grant
+4  A: 
$ time (sleep 5 & sleep 8 & wait)

real    0m8.019s
user    0m0.005s
sys     0m0.005s

Without any arguments, the shell built-in wait waits for all backgrounded jobs to complete.

ephemient