Operating System - HP-UX
1832272 Members
2559 Online
110041 Solutions
New Discussion

script timeout parameters

 
Mark Mischler
New Member

script timeout parameters

Do scripts generally timeout after some predetermined period of time? If so, is this 'timeout' parameter adjustable? I wrote a script consisting of 5 tasks: if one task takes ALL DAY to complete will the script wait ALL DAY for the process or will it move to the next process after X number of minutes?

4 REPLIES 4
Sachin Patel
Honored Contributor

Re: script timeout parameters

Hi Darrian,
It is depends on your script.
for example if you doing remsh from your script it will generally timeout after couple second.
You have to put checking in you script for example that frist you want to display date only 10 times then do task 2 if task 2 fails or if result from task 2 is none after 5 minute then report the error and go on for task 3 or exit from script.

Sachin
Is photography a hobby or another way to spend $
James R. Ferguson
Acclaimed Contributor

Re: script timeout parameters

Hi Darrian:

There are various ways to do what you want.

You can create background processes, monitor them and kill them after a predetermined time if they have not completed. The monitoring can be done using the PID of the background process and a 'kill -0' to test if its still a valid PID.

If you are reading input from 'stdin', the 'line' command has a timeout feature. The command reads one line (up to a newline) from stdin. If nothing is read after (t)imeout then a null string is returned:

#!/usr/bin/sh
while true
do
echo "...reading..."
WHAT=`line -t 5` #...give user 5-seconds to respond
if [ ! -z "$WHAT" ]
then
echo "I read: $WHAT"
fi
done
#.end.

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: script timeout parameters

Hi Darrian,

No the command while block all year until completed. Even's James' suggestion of test with kill -0 pid is not foolproof because on these time scales pid's are recycled.


In your case you could simply do this:

cmd1 &
cmd2 &
cmd3 &
cmd4 &
cmd5 &
wait

This will start all of the commands in background but the script will not exit until
all the background processes complete.

If cmd2 must wait until cmd1 completes but the others can run asynchrously, you could do this:

(cmd1; cmd2) &
cmd3 &
cmd4 &
cmd5 &
wait

If you want really good timeout (alarm) and other signal handlers from a scripting language probably your best bet is perl.

My 2 cents, Clay
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: script timeout parameters

Hi Darrian:

I agree with Clay that one would not want to let long periods of time elapse and then arbitrarily test the vailidity of a PID.

What I meant was a case where you initiate your process in the background; capture its PID using the "$!" variable; and in a loop sleep for a short time (in seconds); waken; test the captured PID; continue if done; or return to the loop until done.

Regards!

...JRF...