Cygwin: How to stop an application after at most a preset amount of time

I am running a large batch job under Cygwin (Win-XP) which is the same application being executed many times with different input data in a for loop. Occasionally, the application hangs. What I want is to automatically interrupt the app if it does finish in predetermined time. One way to do it is something like this:

for ........
       my_app ..... &
       sleep 600
       kill -9 $!

This will launch the app in background, wait for 600 seconds and then kill it if it hasn't finished by itself. The problem is that even if it does finish by itself, this pass of the loop will still complete only after 600 secs. What I want is for it to finish immediately if the application exits normally and only wait for 600 secs if it does not. How can I do it?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can probably do this fine with expect (use a timeout).

-- Glenn
pidof my_app ----- to get process id
kill-9 <pid> ----- to kill them
ilyazAuthor Commented:

I found the following expect script on the Web:

#!/usr/bin/expect -f
# run a program for a given amount of time
# i.e. time 20 long_running_program
set timeout [lindex $argv 0]
eval spawn [lrange $argv 1 end]

which I run like this

expect -f timed-run.exp 5 my_app

Although it exits if my_app finishes early, it does not kill my_app if time is up. Is there a simple modification to make it work? Thanks.
Yes, make an expect clause for the timeout with an abort action.
(I'm unable to test out the syntax right now... Check the expect man-page, there is an example on how it might look... Or wait till tomorrow when I have the time:-).

-- Glenn
Ok, done some tinkering... Try
#!/usr/bin/expect -f
# run a program for a given amount of time
# i.e. time 20 long_running_program
set timeout [lindex $argv 0]
set pid [eval spawn [lrange $argv 1 end]]
expect {
        timeout {exec kill $pid}
# End of script
... instead. Just save that in a file, "chmod +x that_file" and "./that_file your_script your_timeout". Should work OK.
If one'd like to, one should perhaps enhance it with some parameter checks etc:-).

-- Glenn

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.