bash 使用 & 在后台重击不同的循环并等待

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15139565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:41:58  来源:igfitidea点击:

bash different loops in background using & and wait

bashloopsfor-loopwait

提问by fabioln79

I've taken a look to the related topics but I did not found an answer.

我查看了相关主题,但没有找到答案。

Here's my problem:

这是我的问题:

I'm trying to put these commands that I usually run NOT in a for loop into two separate for loops

我试图将这些通常不在 for 循环中运行的命令放入两个单独的 for 循环中

original commands:

原始命令:

command1 &
command2 &

wait

command 3

This obviously starts two commands in background and, after BOTH are finished, it starts the command 3

这显然在后台启动了两个命令,在 BOTH 完成后,它启动了命令 3

Now here there's my for loop script:

现在这是我的 for 循环脚本:

file1=directory1/*.txt

for i in $file1;

do

command1 ${i} > ${i}.test & # I know, it will generate files like ".txt.test". It's ok.

done &


file2=directory2/*.txt

for i2 in $file2;

do

command1 ${i2} > ${i2}.test & 

done &

wait

command3

Now there is somethig wrong in my script because sometimes when is performing the command 3 I can find some jobs from command1 or command2 EVEN if I put the "wait".

现在我的脚本中有一些错误,因为有时在执行命令 3 时,即使我输入“等待”,我也可以从 command1 或 command2 中找到一些作业。

I've tried different option like the second "done" without &. I've tried also two wait..but everything I do...I mess up all the jobs :(

我尝试了不同的选项,比如没有 & 的第二个“完成”。我也试过两次等待......但我所做的一切......我把所有的工作都搞砸了:(

Where is my mistake (please...be polite :P)?

我的错误在哪里(请...礼貌:P)?

Thank you

谢谢

Fabio

法比奥

回答by Radi R

Save both "for" loops, but remove all "&" from them, as separate files:loop1,loop2. chmod a+rx loop1 loop2; and execute:

保存两个“for”循环,但从它们中删除所有“&”,作为单独的文件:loop1,loop2。chmod a+rx loop1 loop2; 并执行:

loop1 &
loop2 &
wait
command3

I don't know the behaviour of "done &", better don't use it.
Your code is executing everything at the same time. I am assumming that you want 2 threads.

我不知道“done &”的行为,最好不要使用它。
您的代码同时执行所有内容。我假设你想要 2 个线程。

Edit: Single script solution:

编辑:单脚本解决方案:

script1=`mktemp /tmp/.script.XXXXXX`;
cat >$script1 <<END
for i in directory1/*.txt; do
  command1 ${i} > ${i}.test;
done 
END

script2=`mktemp /tmp/.script.XXXXXX`;
cat >$script2 <<END
for i in directory2/*.txt; do
  command1 ${i} > ${i}.test;
done 
END

chmod u+rx $script1 $script2
$script1 &
$script2 &
wait;
command3

/bin/rm $script1 $script2

回答by errant.info

There is no need for you to put the ampersand (&) after each done, you can simply put it after each command and all jobs from both loops will be put into the background and a single wait will do the trick.

您无需在每次完成后放置与号 (&),您只需将它放在每个命令之后,两个循环中的所有作业都将被置于后台,一次等待即可解决问题。

for i in $( seq 1 3 ); do
    command1 "$i" > "$i.test" &
done

for i in $( seq 4 6 ); do
    command2 "$i" > "$i.test" &
done

wait

command3

An alternative approach is to store the pid of each background process by making use of $!, like so

另一种方法是通过使用 来存储每个后台进程的 pid $!,就像这样

pid=""
command & pid="$pid $!"
wait $pid