4

Hello I am working with a simulator that uses rcS scripts to boot, this is my script

cd /tests
./test1 &
./test2 &
./test3 &
./test4 
exit

What I want is run all the test at the same time and that the exit command is executed only when all the previous test have finished. And not only when test 4 has finished, is this possible?. Thank you.

Eduardo
  • 19,928
  • 23
  • 65
  • 73
  • You probably want to replace "./exit" with just "exit" to exit the shell script. If you're actually running an executable or script named 'exit' in that directory, I suggest renaming it something less confusing. – Adam Rosenfield Jan 10 '09 at 21:48

3 Answers3

8

You can use wait:

./test1 &
./test2 &
./test3 &
./test4 &
wait

From the bash man page:

wait [n ...] Wait for each specified process and return its termination status. 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 processes 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.

gak
  • 32,061
  • 28
  • 119
  • 154
  • Note, if your trying to run a php script, come configurations of php will prevent processes run in the background from outputting anything. I found out that its hard to Google for how to turn on default behaviors. I eventually found this question: http://serverfault.com/q/564720/210994 – ThorSummoner Jan 15 '15 at 21:39
5

xargs can support parallel

So just like this:

seq 4|xargs -i -n 1 -P 4 ./test{} 
fedorqui
  • 275,237
  • 103
  • 548
  • 598
firejox
  • 137
  • 2
  • 2
  • Which flag is the one that activates parallel execution? And Do all sub processed start at nearly/exactly the same time? – ThorSummoner Jan 15 '15 at 20:55
4

Something along the lines of

cd /tests
./test1 &
./test2 &
./test3 &
./test4 &
wait
exit

(I am assuming bash shell)