【问题标题】:Linux shell script asynchronous commands and notification when completedLinux shell 脚本异步命令和完成时的通知
【发布时间】:2012-08-31 05:59:27
【问题描述】:

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

  1. shell 脚本更新 git 存储库。
  2. shell 脚本停止应用程序服务器。
  3. shell 脚本停止 Web 服务器。
  4. shell 脚本指示应用服务器检查最新的 git 更新。
  5. shell 脚本指示 Web 服务器检查最新的 git 更新。
  6. shell 脚本启动应用服务器。
  7. shell 脚本启动 Web 服务器。

这 7 个步骤中的每一个都一个接一个地同步完成。总运行时间约为 9 秒。然而,为了减少停机时间,其中许多步骤可以异步完成。

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

【问题讨论】:

    标签: linux shell


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案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.
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-03-30
          • 2014-03-27
          • 2011-01-23
          • 2023-02-06
          • 2019-11-06
          • 1970-01-01
          • 2011-08-06
          相关资源
          最近更新 更多