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

Posted on 2005-03-22
Medium Priority
Last Modified: 2010-04-20
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?

Question by:ilyaz
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 20

Expert Comment

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

-- Glenn

Expert Comment

ID: 13601495
pidof my_app ----- to get process id
kill-9 <pid> ----- to kill them

Author Comment

ID: 13602679

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.
LVL 20

Expert Comment

ID: 13604877
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
LVL 20

Accepted Solution

Gns earned 280 total points
ID: 13609617
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

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question