Re: UNIX command for timeout?



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
>
> #!/bin/sh
> # timeout script
> #
> usage()
> {
> echo "usage: timeout seconds command args ..."
> exit 1
> }
>
> [[ $# -lt 2 ]] && usage
> seconds=$1; shift
>
> timeout()
> {
> sleep $seconds
> kill -9 $pid >/dev/null 2>/dev/null
> }
>
> eval "$@" &
> pid=$!
> timeout &
> wait $pid

Wow, that's pretty slick. That's not any longer than the C would be, I
bet.

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



Relevant Pages

  • Re: cybercafe system
    ... A simple bash script could be ran on the system which will cause it to ... text terminal after a timeout period passed in from the command line. ...
    (Ubuntu)
  • Re: How to time out a command
    ... > I have a ksh script that calls another utility (command A). ... I once made the following script to do exactly that. ... # Runs a command and kills it, if necessary, after a given timeout. ... # 0<c<127 - job exited with this exit code ...
    (comp.unix.shell)
  • Re: scripts running all the time?
    ... Write a script that starts the download. ... timeout - Run a command with a time limit. ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx ...
    (Debian-User)
  • SUMMARY: how to capture PID at execution time
    ... The "gotcha" i had was that my script executes more scripts to run the httpd. ... I couldn't capture the pid of commands in sub-scripts. ... > the command will be run at the same time, i need to make sure that the correct ...
    (SunManagers)
  • Re: UNIX command for timeout?
    ... > I'm looking for an easy way to have a UNIX command timeout after a ... > I realize I could approximate it in a shell script, ... > diskutil mount disk0s3 & ... > no guarantee that there's not more than one diskutil command running, ...
    (comp.sys.mac.system)