gpt4 book ai didi

linux - 在 Parallel Bash 中运行串行

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:53 25 4
gpt4 key购买 nike

我添加了一些解释。从概念上讲,我正在运行一个循环处理的脚本,调用使用行内容作为输入参数的 shell。(仅供引用:a 启动执行,b 监视该执行)

  1. 我需要先运行 1a 和 1b,前两个 $param 并行运行
  2. 接下来,当第 1 步完成时,2a 和 2b 需要针对 $params 串行运行
  3. 一旦 2a 和 2b 完成,3a 和 3b 将开始(无论是串行还是并行,都无关)
  4. 循环继续执行输入 .txt 中的接下来 2 行

我无法让它串行处理第二个,只能并行处理:我需要的是以下内容

cat filename | while readline 
export param=$line
do
./script1a.sh "param" > process.lg && ./script2b.sh > monitor.log &&
##wait for processes to finish, running 2 in parallel in script1.sh
./script2a.sh "param" > process2.log && ./script2b.sh > minitor2.log &&
##run each of the 2 in serial for script2.sh
./script3a.sh && ./script3b.sh

我尝试添加 wait,并尝试了包含 script2a.sh 和 script2b.sh 的 if 语句,该语句将串行运行,但无济于事。

if ((++i % 2 ==0)) then wait fi
done
#only run two lines at a time, then cycle back through loop

我到底如何才能让 script2.sh 作为 script1 并行运行的结果而串行运行?

最佳答案

锁定!

如果您想要并行化 script1script3,但需要序列化 ​​script2 的所有调用,请继续使用:

./script1.sh && ./script2.sh && ./script3.sh &

...但修改 script2 以在执行其他操作之前获取锁定:

#!/bin/bash
exec 3>.lock2
flock -x 3
# ... continue with script2's business here.

请注意,您不得删除此处使用的.lock2文件,否则可能会导致多个进程认为它们同时持有锁。

关于linux - 在 Parallel Bash 中运行串行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29953990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com