Linux shell脚本异步命令和通知完成后

时间:2012-08-30 19:58:18

标签: linux shell

我有一个更新Web应用程序的脚本。 Web应用程序分布在两台服务器上。这是脚本的纲要

  1. shell脚本更新git存储库。
  2. shell脚本会停止应用程序服务器。
  3. shell脚本会停止Web服务器。
  4. shell脚本指示应用程序服务器检出最新的git更新。
  5. shell脚本指示Web服务器检出最新的git更新。
  6. shell脚本启动应用程序服务器。
  7. shell脚本启动Web服务器。
  8. 7个步骤中的每个步骤一个接一个地同步完成。总运行时间约为9秒。但是,为减少停机时间,许多步骤可以异步完成。

    例如,步骤4和5可以同时完成。我想异步启动第4步和第5步(例如在后台运行),但我找不到如何等到它们都完成后再继续。

3 个答案:

答案 0 :(得分:10)

您可能希望使用命令分组来维护需要同步的步骤:

step1
( step2 && step4 && step6 ) &
( step3 && step5 && step7 ) &
wait && echo "all done"

答案 1 :(得分:5)

在脚本的后台启动第4步和第5步(结束&),然后在运行第6步之前简单地调用wait bash builtin

答案 2 :(得分:3)

您正在寻找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.
相关问题