Originally posted by: SystemAdmin
The following will start a timer and try to execute a list.
If the timer expires, the list will receive an HUP signal.
If the list completes, the timer will receive an HUP signal.
The lines after 'wait' will be executed,
after the list completes or the timer expires,
whichever comes first.
idiom
...
(tasks;that;might;take;too;long) &
TIMED=$!; (sleep $TIMEOUT; kill -HUP $TIMED) &
TIMER=$!; wait $TIMED; kill -HUP $TIMER
...
idiom
example
-
Author: drb
-
Forum: IBM developerWorks>AIX and UNIX>AIX - technical
-
Execute with and without timeout
for TASKTIME in 5 15
do
TIMEOUT=10
# Start the task that may take too much time
(
while test 0 -lt $TASKTIME
do
echo "$TASKTIME"; sleep 1
let "TASKTIME=TASKTIME-1"
done
echo "COMPLETED"
) &
TIMED=$!
# Start a timer
(
sleep $TIMEOUT
if kill -HUP $TIMED 2> /dev/null; then echo "TIMEOUT"; fi
) &
TIMER=$!
# Wait for completion (or timeout)
wait $TIMED 2> /dev/null
kill -HUP $TIMER 2> /dev/null
# Do other things
if test $TASKTIME -lt $TIMEOUT
then
echo "The tasks should have completed."
else
echo "The timer should have expired."
fi
done
example
Archiving,
#AIX-Forum