|
|
Can I use a sigalrm signal to timeout an operation in a shell script?
I would like to timeout an operation within a shell script. Can I use sigalrm
or another signal for this?
For example:
slowprocess()
{
i=1
while [ "$i" -le 20 ] ; do
i=`expr $i + 1`
sleep 10
done
echo "completed without interuption"
}
# main
trap "echo too slow ; return()" SIGALRM
sleep 20 & # Abort the slow process on completion
slowprocess
echo "end of script"
--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE
Telephone: (0121) 247 1596
International: 0044 121 247 1596
Email: markhobley at hotpop dot donottypethisbit com
http://markhobley.yi.org/
|
|
0
|
|
|
|
Reply
|
markhobley
|
1/15/2006 10:08:28 PM |
|
["Followup-To:" header set to comp.os.linux.]
On Sun, 15 Jan 2006 22:08:28 GMT, Mark Hobley
<markhobley@hotpop.deletethisbit.com> wrote:
> I would like to timeout an operation within a shell script. Can I use sigalrm
> or another signal for this?
>
> # main
>
> trap "echo too slow ; return()" SIGALRM
> sleep 20 & # Abort the slow process on completion
> slowprocess
> echo "end of script"
>
I think you've got that backward:
slowprocess &
sleep 20
kill $!
--
The computer should be doing the hard work. That's what it's paid to do,
after all.
-- Larry Wall in <199709012312.QAA08121@wall.org>
|
|
0
|
|
|
|
Reply
|
Bill
|
1/15/2006 11:42:03 PM
|
|
In alt.comp.os.linux Bill Marcum <bmarcum@iglou.com> wrote:
> I think you've got that backward:
It's not actually working code, just a concept really at the moment, I'm
toying with a few project ideas.
I thought maybe I could background the sleep command, to produce a signal on
completion.
On detection of this signal, I could take some action.
I didn't want to background the slowprocess.
What about if I wanted to obtain user input?
Could I abort the input process if the data was not entered in time?
Even better, could I reset the timer, if a user suddenly started typing?
Regards,
Mark.
--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE
Telephone: (0121) 247 1596
International: 0044 121 247 1596
Email: markhobley at hotpop dot donottypethisbit com
http://markhobley.yi.org/
|
|
0
|
|
|
|
Reply
|
markhobley
|
1/22/2006 2:08:24 AM
|
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[I'm reading this in comp.unix.programmer, and hence assuming you are
using the SUS/POSIX API with C or C++, but looking at the other
newsgroups this originated in, I may be mistaken, in which case I
apologise for the confusion.]
markhobley@hotpop.deletethisbit.com (Mark Hobley) writes:
> In alt.comp.os.linux Bill Marcum <bmarcum@iglou.com> wrote:
>
> What about if I wanted to obtain user input?
>
> Could I abort the input process if the data was not entered in time?
Yes. Consider that system calls can be interrupted by signals (this
can be specified by using sigaction and omitting SA_RESTART to disable
the restarting of interrupted system calls). Therefore, if you use
alarm or setitimer to trigger a SIGALRM at some point in the future, a
read() call will be stopped, and you can detect that. (The signal
handler can be empty in this instance; we are just using it as a means
of interrupting the user input).
See
http://cvs.alioth.debian.org/cgi-bin/cvsweb.cgi/schroot/schroot/sbuild-auth-conv-tty.cc?rev=1.11&content-type=text/x-cvsweb-markup&cvsroot=buildd-tools
for a simple example of this. This is for simple username/password
entry (it's a PAM conversation wrapper), and relies upon the
terminal/termios line buffering. Two separate timeouts are
implemented: a warning timeout and a final fatal timeout.
> Even better, could I reset the timer, if a user suddenly started typing?
Yes. However, you'll need to disable line buffering by tweaking the
termios for the controlling tty, so that you get input
character-by-character instead of line-by-line. I would recommend
using setitimer rather than alarm in this instance, so you can
continually adjust the timer with finer accuracy. You might need to
handle erase/del/bs if you wish to allow the user to correct the
input, or perhaps use ncurses in this instance.
Regards,
Roger
- --
Roger Leigh
Printing on GNU/Linux? http://gutenprint.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8+ <http://mailcrypt.sourceforge.net/>
iD8DBQFD03csVcFcaSW/uEgRArvKAJ9CTSCSNvIOihEmm/EMfkND2cpL5ACdHIUS
cG6XmPcldbvtdhbFmrOS3PQ=
=BPB0
-----END PGP SIGNATURE-----
|
|
0
|
|
|
|
Reply
|
Roger
|
1/22/2006 12:14:39 PM
|
|
|
3 Replies
936 Views
(page loaded in 0.052 seconds)
|
|
|
|
|
|
|
|
|