1

Does the following script

#!/usr/bin/bash

while true do ab -c 700 -n 100000000000 -r http://crimea-post.ru/ >>crimea-post & ab -c 700 -n 100000000000 -r http://nalog.gov.ru/ >>nalog done

do exactly the same as

#!/usr/bin/bash

while true do ab -c 700 -n 100000000000 -r http://crimea-post.ru/ >>crimea-post ab -c 700 -n 100000000000 -r http://nalog.gov.ru/ >>nalog done

From my experience the first script creates nalog file sooner (within seconds) than the second script (after more than 10 minutes) which suggests to me that the latter waits for ab -c 700 -n 100000000000 -r http://nalog.gov.ru/ >>nalog to finish. It should not be the case because from what I've researched so far the second script is meant to start ab -c 700 -n 100000000000 -r http://nalog.gov.ru/ >>nalog without waiting for ab -c 700 -n 100000000000 -r http://crimea-post.ru/ >>crimea-post to finish. I want the two said ab commands to execute concurrently, how might I accomplish this in Bash?

P.S. It is & and not &&. I know what && does and did not want to apply it here.

1 Answers1

6

No, cmd1 [newline] cmd2 is not as concurrent as cmd1 & cmd2 — the former waits for cmd1 to complete before starting cmd2.

If you want execution to continue while the first command runs, you need to keep the & when splitting this over two lines:

ab -c 700 -n 100000000000 -r http://crimea-post.ru/ >>crimea-post & ab -c 700 -n 100000000000 -r http://nalog.gov.ru/ >>nalog

becomes

ab -c 700 -n 100000000000 -r http://crimea-post.ru/ >>crimea-post &
ab -c 700 -n 100000000000 -r http://nalog.gov.ru/ >>nalog

If you want to ensure both commands complete before looping again, you can use wait.

Other shell constructs also provide concurrency, notably pipes: a | b (or its variant in some shells, a |& b, to redirect both output streams¹) runs a and b concurrently, and waits for both to complete before continuing. Some shells also support coprocesses (see How do you use the command coproc in various shells?) and process substitution (see How do you use the command coproc in various shells?) which can be used to run commands concurrently.

See What are the shell's control and redirection operators? for details of the list terminators (which include &) and pipe operators.


¹ Except in ksh, where it runs a as a coprocess.

Stephen Kitt
  • 434,908
  • Note that in ksh, a |& b is like the coproc a; b of zsh / bash, which is different from the a |& b of csh / zsh / bash (though still run a and b concurrently) – Stéphane Chazelas Feb 28 '22 at 08:43
  • @StéphaneChazelas aargh, there’s always more variants to deal with... – Stephen Kitt Feb 28 '22 at 08:46
  • That "canonical answer" is geared towards the bash shell (as it says in the introduction) and POSIX sh though, it's not covering the many more operators found in some other Bourne-like shells such as ksh93 or zsh nor the corresponding operators of non-Bourne-like shells. – Stéphane Chazelas Feb 28 '22 at 10:06
  • Good point, and providing detailed coverage of all that would make the answer more complex and therefore less useful. – Stephen Kitt Feb 28 '22 at 10:09