Discussion:
need bash command timeout help
(too old to reply)
Todd
2011-02-21 01:12:57 UTC
Permalink
Hi All,

I have a bash script with the following command in it.

Error="`su $DefaultUser -c "/usr/bin/VBoxManage \
--nologo guestcontrol execute "$VM_Name" \
'C:\Windows\system32\shutdown.exe' \
--arguments '/f /t 5 /s /d p:2:3' \
--username $WinAdminUser \
--password $WinAdminPassword \
--wait-for stdout" | \
grep ERROR`"

Problem, VBoxManage occasionally never completes and
the command hangs. The rest of the script will not
complete. And that creates a big problem.

If I put a "&" at the end of the grep command, it will still
have to wait for "Error=" to populate, so I am stuck.

Anyone know a way to run VBoxManage such that it will
time out if the command seizes?

Many thanks,
-T
Harald Meyer
2011-02-21 01:42:03 UTC
Permalink
Post by Todd
Anyone know a way to run VBoxManage such that it will
time out if the command seizes?
Maybe I didn't fully understand your problem, but are you aware
that there is a "timeout" utility? (in GNU coreutils)
Todd
2011-02-21 02:07:50 UTC
Permalink
Post by Harald Meyer
Post by Todd
Anyone know a way to run VBoxManage such that it will
time out if the command seizes?
Maybe I didn't fully understand your problem,
If I execute an external command from a bash script, and
the command hangs, I want a way to force a timeout so my
script can continue.
Post by Harald Meyer
but are you aware
that there is a "timeout" utility? (in GNU coreutils)
No I did not know. I am interested.

I have "coreutils":
# rpm -qa \*coreutils\*
coreutils-5.97-23.el5_4.2

But not "timeout"
$ timeout --help
bash: timeout: command not found

But I do have:
/usr/share/doc/bash-3.2/scripts/timeout
Is this the one your are talking about? Sure seems
like it.

Many thanks,
-T
Todd
2011-02-21 02:12:00 UTC
Permalink
Post by Todd
/usr/share/doc/bash-3.2/scripts/timeout
Is this the one your are talking about? Sure seems
like it.
Many thanks,
timeout -KILL 5 sleep 20
Killed

This is what I am looking for. Thank you!

-T
Harald Meyer
2011-02-21 02:22:51 UTC
Permalink
Post by Todd
Post by Harald Meyer
but are you aware
that there is a "timeout" utility? (in GNU coreutils)
No I did not know. I am interested.
# rpm -qa \*coreutils\*
coreutils-5.97-23.el5_4.2
It's 8.5-1 in current Ubuntu 10.10
Post by Todd
But not "timeout"
$ timeout --help
bash: timeout: command not found
| $ which timeout
| /usr/bin/timeout
| $ whatis timeout
| timeout (1) - run a command with a time limit
| $ dpkg -S /usr/bin/timeout
| coreutils: /usr/bin/timeout
| $ file /usr/bin/timeout
| /usr/bin/timeout: ELF 32-bit LSB executable, ...
Post by Todd
/usr/share/doc/bash-3.2/scripts/timeout
Is this the one your are talking about? Sure seems
like it.
No, I don't know that script. I discovered "timeout" myself only recently,
don't know in which version it appeared.
Todd
2011-02-21 02:31:11 UTC
Permalink
Post by Harald Meyer
Post by Todd
Post by Harald Meyer
but are you aware
that there is a "timeout" utility? (in GNU coreutils)
No I did not know. I am interested.
# rpm -qa \*coreutils\*
coreutils-5.97-23.el5_4.2
It's 8.5-1 in current Ubuntu 10.10
Post by Todd
But not "timeout"
$ timeout --help
bash: timeout: command not found
| $ which timeout
| /usr/bin/timeout
| $ whatis timeout
| timeout (1) - run a command with a time limit
| $ dpkg -S /usr/bin/timeout
| coreutils: /usr/bin/timeout
| $ file /usr/bin/timeout
| /usr/bin/timeout: ELF 32-bit LSB executable, ...
Post by Todd
/usr/share/doc/bash-3.2/scripts/timeout
Is this the one your are talking about? Sure seems
like it.
No, I don't know that script. I discovered "timeout" myself only recently,
don't know in which version it appeared.
I am on old-out-of-date (Cent OS 5.5). CentOS 6.0 is due out any
day now, so maybe ...

The script seems to work fine.

Many thanks,
-T

/usr/share/doc/bash-3.2/scripts/timeout:

#Newsgroups: comp.unix.admin,comp.unix.solaris,comp.unix.shell
#From: ***@root.co.uk (Geoff Clare)
#Subject: Re: timeout -t <sec> <unix command> (Re: How to give rsh a #
shorter timeout?)
#Message-ID: <***@root.co.uk>
#Date: Fri, 13 Feb 1998 18:23:52 GMT

#
# Conversion to bash v2 syntax done by Chet Ramey <***@po.cwru.edu
# UNTESTED
#

prog=${0##*/}
usage="usage: $prog [-signal] [timeout] [:interval] [+delay] \
[--] <command>"

SIG=-TERM # default signal sent to the process when the timer expires
timeout=60 # default timeout
interval=15 # default interval between checks if the process is
still alive
delay=2 # default delay between posting the given signal and
# destroying the process (kill -KILL)

while :
do
case $1 in
--) shift; break ;;
-*) SIG=$1 ;;
[0-9]*) timeout=$1 ;;
:*) EXPR='..\(.*\)' ; interval=`expr x"$1" : "$EXPR"` ;;
+*) EXPR='..\(.*\)' ; delay=`expr x"$1" : "$EXPR"` ;;
*) break ;;
esac
shift
done

case $# in
0) echo "$prog: $usage" >&2 ; exit 2 ;;
esac

(
for t in $timeout $delay
do
while (( $t > $interval ))
do
sleep $interval
kill -0 $$ || exit
t=$(( $t - $interval ))
done
sleep $t
kill $SIG $$ && kill -0 $$ || exit
SIG=-KILL
done
) 2> /dev/null &

exec "$@"
Todd
2011-02-21 04:20:15 UTC
Permalink
Post by Harald Meyer
Post by Harald Meyer
but are you aware
Post by Harald Meyer
that there is a "timeout" utility? (in GNU coreutils)
No I did not know. I am interested.
# rpm -qa \*coreutils\*
coreutils-5.97-23.el5_4.2
It's 8.5-1 in current Ubuntu 10.10
Post by Harald Meyer
But not "timeout"
$ timeout --help
bash: timeout: command not found
| $ which timeout
| /usr/bin/timeout
| $ whatis timeout
| timeout (1) - run a command with a time limit
| $ dpkg -S /usr/bin/timeout
| coreutils: /usr/bin/timeout
| $ file /usr/bin/timeout
| /usr/bin/timeout: ELF 32-bit LSB executable, ...
Post by Harald Meyer
/usr/share/doc/bash-3.2/scripts/timeout
Is this the one your are talking about? Sure seems
like it.
Just added a call to the script version in my script.
Works perfectly. Thanks for pointing me in the
right direction!

-T
Lawrence D'Oliveiro
2011-02-23 03:53:24 UTC
Permalink
... but are you aware that there is a "timeout" utility? (in GNU
coreutils)
LSNED! :)
Todd
2011-02-23 20:14:50 UTC
Permalink
Post by Lawrence D'Oliveiro
... but are you aware that there is a "timeout" utility? (in GNU
coreutils)
LSNED! :)
What?
Harald Meyer
2011-02-23 21:14:50 UTC
Permalink
Post by Lawrence D'Oliveiro
LSNED! :)
What?
Learn Something New Every Day :)
Todd
2011-02-23 22:02:00 UTC
Permalink
Post by Harald Meyer
Post by Lawrence D'Oliveiro
LSNED! :)
What?
Learn Something New Every Day :)
Guess I did. "which lsned" didn't find anything. :'[

-T
Lawrence D'Oliveiro
2011-02-23 23:35:09 UTC
Permalink
Post by Todd
Post by Harald Meyer
Post by Lawrence D'Oliveiro
LSNED! :)
What?
Learn Something New Every Day :)
Guess I did. "which lsned" didn't find anything. :'[
And to think I just made that up. :)
Todd
2011-02-24 04:13:29 UTC
Permalink
Post by Lawrence D'Oliveiro
Post by Todd
Post by Harald Meyer
Post by Lawrence D'Oliveiro
LSNED! :)
What?
Learn Something New Every Day :)
Guess I did. "which lsned" didn't find anything. :'[
And to think I just made that up. :)
It was the "ls" that got me. :-)

-T

$ ls /usr/bin/ls*
/usr/bin/lsattr /usr/bin/lsdiff /usr/bin/lshal /usr/bin/lss16toppm
/usr/bin/lsb_release /usr/bin/lsdvd /usr/bin/lspgpot /usr/bin/lsscsi
Bill Marcum
2011-02-21 02:03:11 UTC
Permalink
Post by Todd
Hi All,
I have a bash script with the following command in it.
Error="`su $DefaultUser -c "/usr/bin/VBoxManage \
--nologo guestcontrol execute "$VM_Name" \
'C:\Windows\system32\shutdown.exe' \
--arguments '/f /t 5 /s /d p:2:3' \
--username $WinAdminUser \
--password $WinAdminPassword \
--wait-for stdout" | \
grep ERROR`"
Problem, VBoxManage occasionally never completes and
the command hangs. The rest of the script will not
complete. And that creates a big problem.
If I put a "&" at the end of the grep command, it will still
have to wait for "Error=" to populate, so I am stuck.
Anyone know a way to run VBoxManage such that it will
time out if the command seizes?
Many thanks,
-T
If your system has the killall command you could do this:
( sleep $Timeout; su $DefaultUser -c "killall VBoxManage" ) &
--
Science is to computer science as hydrodynamics is to plumbing.
Loading...