1829596 Members
1698 Online
109992 Solutions
New Discussion

Timeout on a script

 
SOLVED
Go to solution
Ravi S. Banda
Regular Advisor

Timeout on a script

I have a UNIX script that is run to stop tuxedo processes gracefully. But, not all the time it completes normal. Sometimes, the script just hangs! So, I open another session, run a kill script that kills all the remaining tuxedo processes and also kill the shared memory identifiers.

Question: How would I put a timeout on the script, so if the 'stop' script has run for 5-10 minutes, I want the control back? I can take appropriate action.

Thanks!
Ravi.
4 REPLIES 4
Dennis Handly
Acclaimed Contributor
Solution

Re: Timeout on a script

You could do the action in the background and then sleep for 10 minutes and then see if that background process is still there.
Ravi S. Banda
Regular Advisor

Re: Timeout on a script

good point!
totally overlooked.
assigning points.

Thanks!
Ravi.
Viktor Balogh
Honored Contributor

Re: Timeout on a script

i would check in the process list what is going on and why it hangs. just grep in the output of 'ps -ef' and traverse through the parent-child processes to see what is it doing now, for what child process it is waiting. if you could identify the child process, do a 'tusc -p PID_of_child' to see what is going on deep down... ;)

handing the control back would be just a workaround, i would prefer clear it what is the hard part by stopping the processes.
****
Unix operates with beer.
Hakki Aydin Ucar
Honored Contributor

Re: Timeout on a script

As far as I know Unix Shell do not have in built facility for timeout a command. You have to use C OR Perl programming. For example check following, you can see C has a function;

# man timeout

So you need a trick as a sleep command and kill process you want ,like this clause:

SetTimer() {
DEF_TOUT=${1:-10};
if [ $DEF_TOUT -ne 0 ] ; then
sleep $DEF_TOUT && kill -s 14 $$ &
CHPROCIDS="$CHPROCIDS $!"
TIMERPROC=$!
fi
}

after 10 sec. it will issue a sleep command with a timeout and use the kill command to send the shell's process ID (stored in $$)