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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Putting a time out on a command - making sure it doesn't run to long 2

Status
Not open for further replies.

Larshg

Programmer
Joined
Mar 1, 2001
Messages
187
Location
DK
I want to put a time out on a command or a script.

it I run a command an it takes more the 30 sec. then it should be killede/stop

This option is ment for a print of the output from a command, and if the command takes more then X sec it should stop, and maby echo "the command timed out"

Thanks

/Lars
 
Try something like this:
1) Called script:
Code:
[ "$timeout" ] && {
  [ $timeout -lt 2 ] && timeout=2
  (sleep $timeout; kill -15 $$; echo "Time out") &
}
#your command here
[ "$timeout" ] && exec kill $! >/dev/null 2>&1
2) Calling script:
Code:
timeout=30 yourcalledscript arglist

Hope This Help
PH.
 
Sweet. Also, if you're going to be passing arguments to this script, remember to use $@ to pass the script arguments to your program.

Like if your program is grep,

Code:
grep $@

where your command is.

-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top