Re: UNIX command for timeout?
- From: Jerry Kindall <jerrykindall@xxxxxxxxxxxxxx>
- Date: Fri, 02 Dec 2005 22:30:08 -0800
In article <nospam.News.Bob-CB8851.21200902122005@xxxxxxxxxxxxxxxx>,
Bob Harris <nospam.News.Bob@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> In article <011220052009253566%jerrykindall@xxxxxxxxxxxxxx>,
> Jerry Kindall <jerrykindall@xxxxxxxxxxxxxx> wrote:
> > I'm looking for an easy way to have a UNIX command timeout after a
> > period of time. That is, something like this:
> > timeout 10 'diskutil mount disk0s3'
> > I realize I could approximate it in a shell script, e.g.
> > diskutil mount disk0s3 &
> > sleep 10
> > killall diskutil
> > ... but that has some disadvantages I dislike. First of all, there's
> > no guarantee that there's not more than one diskutil command running,
> > so the killall might be overly broad. Second, the script always waits
> > 10 seconds whereas it really should continue as soon as the command
> > completes. And then there are the spurious error messages. I could
> > work around these issues, but then the script gets more complicated...
> > This would be nearly trivial to write from scratch in C, using alarm(),
> > but I thought I'd check to see if anyone else already has before I
> > bothered... my C's fairly rusty.
> Here is a script that will do what you want. The only nasty side
> effect is that you may have issues with $ ' " \ characters in the
> command you are executing.
> Bob Harris
> # timeout script
> echo "usage: timeout seconds command args ..."
> exit 1
> [[ $# -lt 2 ]] && usage
> seconds=$1; shift
> sleep $seconds
> kill -9 $pid >/dev/null 2>/dev/null
> eval "$@" &
> timeout &
> wait $pid
Wow, that's pretty slick. That's not any longer than the C would be, I
Jerry Kindall, Seattle, WA <http://www.jerrykindall.com/>
Send only plain text messages under 32K to the Reply-To address.
This mailbox is filtered aggressively to thwart spam and viruses.
- Prev by Date: Re: 10.4.3 and force quit
- Next by Date: Re: CSS and Safari - Help Please
- Previous by thread: Re: UNIX command for timeout?
- Next by thread: Re: UNIX command for timeout?