Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

linux - Run Serial inside Paralell Bash

I have added to my explanation a bit. Conceptually, I am running a script that processes in a loop, calling shells that use the line content as an input parameter.(FYI: a kicks off an execution and b monitors that execution)

  1. I am needing 1a and 1b to run first, in paralell for the first two $param
  2. Next, 2a and 2b need to run in serial for $params when step 1 is complete
  3. 3a and 3b will kick off once 2a and 2b are complete (irrelevant if serial or parallel)
  4. Loop continues with next 2 lines from input .txt

I cant get it to process the second in serial, only all in parallel: What I need is the following

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

I tried adding in wait, and tried an if statement containing script2a.sh and script2b.sh that would run in serial, but to no avail.

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

How on earth can I get the script2.sh to run in serial as a result of script1 in parallel??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Locking!

If you want to parallelize script1 and script3, but need all invocations of script2 to be serialized, continue to use:

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

...but modify script2 to grab a lock before it does anything else:

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

Note that you must not delete the .lock2 file used here, at risk of allowing multiple processes to think they hold the lock concurrently.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...