2013-03-01 32 views
1

如何運行如下所示的幾個命令,以便在完成所有背景運行後執行最後一行(清除)?同時運行基本命令,然後在完成所有命令後依次運行

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

當然回聲命令我不同的,需要很長的時間才能完成(我可以手動或使用其他腳本我知道刪除的文件,但我想知道如何在一個腳本這件事。 )

謝謝!

回答

2

the docs

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 
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 
相關問題