[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How difficult would it be to add a timeout to "wait"?
From: |
Chet Ramey |
Subject: |
Re: How difficult would it be to add a timeout to "wait"? |
Date: |
Thu, 20 Apr 2023 15:28:12 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 |
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 chet@case.edu http://tiswww.cwru.edu/~chet/