1

以下のようにいくつかのコマンドを実行して、バックグラウンドのすべてのコマンドが実行された後に最後の行が実行 (クリーンアップ) されるようにするにはどうすればよいですか?

echo "oyoy 1" > file1 &
echo "yoyoyo 2" > file2 &
rm -f file1 file2

もちろん、エコーコマンドは私にとっては異なり、完了するまでに長い時間がかかります(ファイルを手動で、または知っている別のスクリプトで削除できますが、1つのスクリプトでこれを行う方法を知りたいと思っていました..)

ありがとう!

4

2 に答える 2

2

ドキュメントから

 wait [n ...]
      Wait  for each specified process and return its termination sta-
      tus.  Each n may be a process ID or a job  specification;  if  a
      job  spec  is  given,  all  processes in that job's pipeline are
      waited for.  If n is not given, all currently active child  pro-
      cesses  are  waited  for,  and  the return status is zero.  If n
      specifies a non-existent process or job, the  return  status  is
      127.   Otherwise,  the  return  status is the exit status of the
      last process or job waited for.

したがって、次のように baackground プロセスが完了するのを待つことができます。

echo "oyoy 1" > file1 &
echo "yoyoyo 2" > file2 &
wait
rm -f file1 file2
于 2013-03-01T22:41:39.503 に答える
0

あるいは、実行しているものがたくさんあり、いくつかのプロセスが完了するのを待つだけでよい場合は、pid のリストを保存して、それらを待つだけです。

echo "This is going to take forever" > file1 &
mypids=$!
echo "I don't care when this finishes" > tmpfile &
echo "This is going to take forever also" >file2 &
mypids="$mypids $!"
wait $mypids
于 2013-03-02T23:22:09.833 に答える