如何在 Shell 中运行两个并行脚本但等待“中间”脚本退出

如何在 Shell 中运行两个并行脚本但等待“中间”脚本退出

我想要:

  1. 运行一个永不退出的脚本,这将生成一个文件
  2. 运行一个监视该文件的脚本,然后退出
  3. 运行最终的脚本,该脚本也永远不会退出(这取决于第一个脚本的输出)

我看过这个答案:并行运行命令并等待一组命令完成后再开始下一组命令但我认为我的要求不一样,因为第一个脚本永远不会退出。

# This runs continuously
nps $DEV_SCRIPT &

# How can I not run the final parallel script until this exits?
node ./scripts/waitForAssetManifest.js &
wait

# This also runs continuously
node-dev build/lib/index.js

答案1

您可以使用$!在将作业放入后台后立即获取作业的 pid,并专门等待该进程:

# This runs continuously
nps $DEV_SCRIPT &

# How can I not run the final parallel script until this exits?
node ./scripts/waitForAssetManifest.js &
wait $!

# This also runs continuously
node-dev build/lib/index.js

在这种特定情况下,您最好不要在后台运行第二个命令:

# This runs continuously
nps $DEV_SCRIPT &

# How can I not run the final parallel script until this exits?
node ./scripts/waitForAssetManifest.js

# This also runs continuously
node-dev build/lib/index.js

相关内容