Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

578 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