Alex Parker
Alex Parker

Reputation: 1583

Wait until multiple processes are running

I want to wait until three processes are running, so I am using an until loop. This works when checking one process:

until pgrep my-process; do echo "waiting…"; done

However, I am unable to add additional conditions. If I wrap the condition in single or double square brackets, I get errors. Specifically, these both fail.

until [[ pgrep my-process ]]; do echo "waiting…"; done
until [ pgrep my-process ]; do echo "waiting…"; done

I am unsure how to do multiple conditions if square brackets aren't working for the first case.

Upvotes: 0

Views: 381

Answers (3)

tk421
tk421

Reputation: 5947

Try:

until (( `pgrep my-process |wc -l` > 2 )); do
  echo "waiting..."
  sleep 1
done

The (( notation is for numeric comparisons. The wc -l shows number of lines which in this case means number of processes.

Upvotes: 0

Daniel H
Daniel H

Reputation: 7433

Square brackets aren't used for command grouping. They actually do a completely different set of conditionals; you can use square brackets for just one condition, and you can do multiple conditions without square brackets.

What until, while, etc. all do is they run the command specified (pgrep my-process in your example) and do the condition based on the command's exit code. [ and [[ are just specific examples of commands that are often useful for testing things.

To combine multiple conditions, you can use && for "and" and || for "or" (the [[ and [ commands have their own internal syntax for combining but you could also use && and || for them if you wanted). For more complex operations, you can use parentheses for grouping.

Upvotes: 0

Charles Duffy
Charles Duffy

Reputation: 295403

Square brackets are not involved in combining multiple conditions.

until pgrep my-process && pgrep my-other-process && pgrep my-last-process; do
  echo "waiting..."
  sleep 1
done

Upvotes: 3

Related Questions