Operating System - HP-UX
1827892 Members
1923 Online
109969 Solutions
New Discussion

Re: how to set time-outs for commands in a script

 
SOLVED
Go to solution
Jdamian
Respected Contributor

how to set time-outs for commands in a script

Hi

Sometimes I need to set a time-out for a command line in a critical script.

is there any trick to achieve that ?
7 REPLIES 7
Oviwan
Honored Contributor

Re: how to set time-outs for commands in a script

Hey

check "man sleep" and "man wait"

Regards
Ralph Grothe
Honored Contributor

Re: how to set time-outs for commands in a script

I don't think sleep and wait is what you want.
sleep would simply block the execution of the next command for the number of seconds passed to sleep, while wait would block until all backgrounded, or that whose PID was passed to wait, have finished.
In Perl scripts one usually sets up a signal handler for SIGALRM (at least one could do this with a shell trap) in an eval block and triggers the alarm() syscall.
I wonder if the shell offers a similar scriptable solution, or if one would have to resort to a C or Perl "plugin"?
Madness, thy name is system administration
James R. Ferguson
Acclaimed Contributor

Re: how to set time-outs for commands in a script

Hi Oscar:

In a shell script you can use 'line -t' like:

# REPLY=`line -t 10`
# [ -z "${REPLY}" ] && echo "You didn't enter anything!"

The argument to '-t' is an integer number of seconds to wait on a read of data before timing out.

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: how to set time-outs for commands in a script

What's your "command line"?

There are always many details to be worked
out, but did you mean something like this?

urtx> cat ./trap_test.sh
#!/bin/sh

# Establish a no-op SIGCHLD handler.
trap ":" 20

# Display date-time, and set the timer.
date
sleep 10 &

# Begin the time-uncertain command sequence.
echo 'question? \c'
read ans
echo "ans: >${ans}<"

# Display date-time after the time-uncertain command sequence.
date

urtx> ./trap_test.sh
Thu Apr 19 08:36:39 CDT 2007
question? a
ans: >a<
Thu Apr 19 08:36:40 CDT 2007

urtx> ./trap_test.sh
Thu Apr 19 08:36:50 CDT 2007
question?
ans: ><
Thu Apr 19 08:37:00 CDT 2007

James R. Ferguson
Acclaimed Contributor

Re: how to set time-outs for commands in a script

Hi Oscar:

Ping. Perhaps you will evaluate the latest responses.

Regards!

...JRF...

Bill Hassell
Honored Contributor
Solution

Re: how to set time-outs for commands in a script

You did not specify whether the timeout is related to waitning for an interactive response from a user, or to watch certain processes called by the script and kill them if it runs too long. There is no script-wide timeout. For a process that might take too long, you put the process into the background, then watch the process to see if it completes within a reasonable length of time, and if not, kill the process. This is not simple scripting but can be done.


Bill Hassell, sysadmin
Jdamian
Respected Contributor

Re: how to set time-outs for commands in a script

.