简体   繁体   中英

how to make command run concurrently when programming shell scripts?

i need download two project, namely A and B, the final result is the build of B project, but it can only be build for project A is installed.

so the tasks i should finish is as follows:

  1. download A
  2. download B
  3. build A, then install A
  4. build B, then install B

1 and 2 can run concurrently, 2 and 3 and run concurrently,

the traditional way to program is:

1 && 3
2 && 4

is there better ways to program shell, which can finish much more quickly?

( (1 && 3 )  & 2 )  && 4    // is there possible? or will this be quicker than the former one?

so what is the way to make these task run concurrently?

You can use the traditional way, putting each &&-list in the background (thanks to William for pointing out the correct dependencies to me):

1 && 3 &
2 && wait && 4

First we run the list 1 && 3 in the background. Job 3 will only start once job 1 completes successfully.

Next, start a new list in the background. This list runs job 2, and if it succeeds, calls wait , which will wait for all background jobs to finish. (I don't quite see the exact sequence, but it appears that it will not wait for the background job of which it is part. That's good, since you'd have a case of deadlock where a job is waiting for itself to complete. UPDATE: the second list must run in the foreground, so that the wait command sees the background command as a background job.) After jobs 1, 2 and 3 all complete, wait completes successfully and job 4 begins.

Put them in the background, then use wait:

1&
2&
wait
3&
4&
wait

If you give wait a PID or job spec, then it will give you the exit status for logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM