AIX

AIX

Connect with fellow AIX users and experts to gain knowledge, share insights, and solve problems.


#Power
#Power
#Operatingsystems
#Servers
 View Only
  • 1.  How to timeout in shell script

    Posted 08/30/07 10:33 AM

    Originally posted by: SystemAdmin


    Is there an easy idiom for imposing a timeout on
    a section of shell script?

    I have a list of commands in a /bin/sh script
    do-something
    do-something-else
    do-some-more

    The do-something-else (e.g. db2) may wait indefinitely
    for input.

    What I want to happen is:
    1. start a countdown timer for T seconds
    2. begin executing do-something, do-something-else, ...
    3. if the timer reaches 0
    send SIGHUP to the execution in step 2
    4. if execution in step 2 completes
    cleanly dispose of the timer.

    How can a timeout limit like the above
    be done cleanly within a shell script?

    I vaguely remember using some combination of fork(1),
    exec(1), ??? to do this in the past.

    TIA,

    #AIX-Forum


  • 2.  Re: How to timeout in shell script

    Posted 08/30/07 01:22 PM

    Originally posted by: SystemAdmin


    This is additional data that may narrow the question.
    It can be safely ignored, if there is a simpler idiom.

    Letting my swiss-cheese memory direct my fingers,
    I started piecing together a test case.

    It works, except that:
    even when the task is "COMPLETED" (e.g. 5 sec)
    in less than the timeout period (e.g. 30 sec),
    the script is not "finished" until after the sleep completes
    (30 sec rather than desired 5 sec).

    I vaguely remember wait(1) being involved.

    To use the example, execute it twice,
    once with TIMEOUT and TASKTIME set in favor of TIMEOUT
    and once in favor of TASKTIME.

    • example code --
    TIMEOUT=30
    TASKTIME=5

    1. Start the task that may take too long
    START=$(date)
    (
    while test 0 -lt $TASKTIME; do
    echo "task in progress"; sleep 1
    let "TASKTIME=TASKTIME-1"
    done
    echo "COMPLETED"
    ) &
    CHILD=$!

    1. Start a watchdog
    sleep $TIMEOUT
    if kill -HUP $CHILD 2>/dev/null; then echo "TIMEOUT"; fi

    echo "finished"
    • example code --

    TIA,
    #AIX-Forum


  • 3.  Re: How to timeout in shell script

    Posted 08/31/07 02:20 PM

    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
    1. Author: drb
    2. Forum: IBM developerWorks>AIX and UNIX>AIX - technical

    1. 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