0

In my c-shell file, I did something like:

xterm -e "source xxxx0" &

xterm -e "source xxxx1" &

wait

code....

It works fine that code after "wait" will be executed after the two xterm finished.

But it also gives problem. If I have some ps open, like an txt file opened by geditn, or I have an eclipse open, it will hung there since "wait" is waiting for all jobs to be finished.

So how can I let "wait" only wait for those two xterm jobs.

Or in another word, I want those two "xterm" run concurrently. And after they both done, the code can be continued. How shall I do that?

Thank you very much

thundium
  • 995
  • 4
  • 12
  • 30

1 Answers1

2

Tell wait the process ids it should wait for:

xterm ... &
pid1=$!

xterm ... &
pid2=$!

wait $pid1 $pid2

That's assuming you use a shell where wait supports multiple arguments (bash,zsh, ...). If you use sh for portability, then you have to do two waits:

wait $pid1
wait $pid2

This will wait untill both are finished. If the second finishes before the first, the wait will immediately return.

ahilsend
  • 931
  • 6
  • 15
  • Hi @ahilsend, I found if I use the PID number directly, it works, like: wait $20234 Well if I save the number like: sleep 20 & set pid1=$! sleep 30 & set pid2=$! wait $pid1 $pid2 It says wait: Too many arguments – thundium Aug 07 '13 at 11:53
  • bash supports to wait for multiple pids. Are you using sh? If it doesn't have to be very portable then change to bash. Otherwise do two commands: `wait $pid1; wait $pid2;`This will wait till both are finished. If the second finishes before the first it will give you a message that it's not a child, but immediately return. – ahilsend Aug 07 '13 at 12:05
  • Well, i am using C Shell.Since this works for me: wait $20234 (20234 is a PID I got from the console for sleep 30 &) Well when I save it like "pid1 = $!" I guess I shall use "wait $($pid1)" but $($pid1) is not allowed, it should be easy to make it right? – thundium Aug 07 '13 at 12:17
  • wait $19601 $19602 also fine for me. – thundium Aug 07 '13 at 12:26
  • I see, never used c shell. In that case try \$$pid1. – ahilsend Aug 07 '13 at 12:47
  • :( no, \$$pid1 not working. I'll try to google it. Thanks a lot for your kind help – thundium Aug 07 '13 at 12:50