Bash: wait with timeout
06:59 05 Apr 2012

In a Bash script, I would like to do something like:

app1 &
pidApp1=$!
app2 &
pidApp2=$!

timeout 60 wait $pidApp1 $pidApp2
kill -9 $pidApp1 $pidApp2

I.e., launch two applications in the background, and give them 60 seconds to complete their work. Then, if they don't finish within that interval, kill them.

Unfortunately, the above does not work, since timeout is an executable, while wait is a shell command. I tried changing it to:

timeout 60 bash -c wait $pidApp1 $pidApp2

But this still does not work, since wait can only be called on a PID launched within the same shell.

Any ideas?

linux bash shell timeout waitpid