Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timeout Function 1

Status
Not open for further replies.

BryanY

MIS
Aug 18, 2001
54
US
I'm trying to write some sort of function so that if too much time goes by (20 seconds) the script will break and exit.

But i'm not sure about the best way to do that.
 
In C you would set a timer and a signal handler, I don't think you can do that nicely in a shell script.

If you never wanted the script to run for more than 120 seconds you could put something in the background like ( sleep 120; kill $$ )&, but you couldn't reset that timer and it would hang around even if the script terminated.
 
The alarm fucntion sounds very interesting, (especially since I'm using ksh).

I read the man page, but I'm still kind of confused as to how to use it. Any tips?
 
You may try something like this:
1) Called script:[tt]
[ "$timeout" ] && {
[ $timeout -lt 2 ] && timeout=2
(sleep $timeout; kill -15 $$; echo "Time out") &
}
#your command here
[ "$timeout" ] && exec kill $! >/dev/null 2>&1[/tt]
2) Calling script:[tt]
timeout=30 yourcalledscript arglist[/tt]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I tried some tests with the alarm function, but couldn't get it to work as advertised. I need to dig out my KSH book to read it but I found a posting from David Korn on a forum elsewhere. His recollection of how it worked didn't pan out for me.

Also, I wouldn't even bother trying to use it in a ksh clone, get the real ksh from David's website.
 
just curious..... what's this "forum elsewhere"?

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I googled for "ksh alarm" and found a uwin users forum at research.att.com. There was an archived post from 1998 by Dave talking about the function.
 
gracias!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top