On 4/20/23 1:53 PM, Dale R. Worley wrote:
How difficult would it be to add an optional timeout argument to the
"wait" builtin?

Try a variant of this.

trap 'echo timeout!' USR1 # choose your fighter

wait_with_timeout()
{
        pid=$1
        timeout=$2

        { sleep $timeout && kill -USR1 $$; } & tpid=$!

        wait $pid
        stat=$?

        kill -TERM $tpid 2>/dev/null

        return $stat
}

{ sleep 10; exit 42; } &
pid=$!
wait_with_timeout $pid 5        # will time out
echo $?

wait_with_timeout $pid 10       # won't time out
echo $?


--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    c...@case.edu    http://tiswww.cwru.edu/~chet/


Reply via email to