[UFO Chicago] Shell scripting, timeouts

Jesse Becker jesse_becker@yahoo.com
Thu, 13 Feb 2003 13:11:50 -0800 (PST)


--0-1145242044-1045170710=:72069
Content-Type: text/plain; charset=us-ascii
Content-Id: 
Content-Disposition: inline

--- Ian Bicking <ianb@colorstudy.com> wrote:
> How might I set up a timeout for it?  I'd like to make a
> shell-script wrapper that kills the program if it takes 
> too long.
<snip>
> Is there an easier/better way?

I've attached a plain text file to this email that has a
small watchdog-like program.  I just whipped it up in the
last hour or so, but I think it works...  

It should compile with 'gcc -o watchdog sigalarm.c', but
I've only tested it on a Linux 2.4 box.

Lemme know...

--Jesse

__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com
--0-1145242044-1045170710=:72069
Content-Type: application/x-unknown; name="sigalarm.c"
Content-Transfer-Encoding: base64
Content-Description: sigalarm.c
Content-Disposition: attachment; filename="sigalarm.c"

LyoKCnNpZ2FsYXJtIC0tIGJ5IEplc3NlIEJlY2tlcgogCkNvcHlyaWdodCAo
YykgMjAwMywgSmVzc2UgQmVja2VyIChqYmVja2VyIEAgbm9ydGh3ZXN0ZXJu
LmVkdSkKQWxsIHJpZ2h0cyByZXNlcnZlZC4KClJlZGlzdHJpYnV0aW9uIGFu
ZCB1c2UgaW4gc291cmNlIGFuZCBiaW5hcnkgZm9ybXMsIHdpdGggb3Igd2l0
aG91dCBtb2RpZmljYXRpb24sIGFyZQpwZXJtaXR0ZWQgcHJvdmlkZWQgdGhh
dCB0aGUgZm9sbG93aW5nIGNvbmRpdGlvbnMgYXJlIG1ldDoKClJlZGlzdHJp
YnV0aW9ucyBvZiBzb3VyY2UgY29kZSBtdXN0IHJldGFpbiB0aGUgYWJvdmUg
Y29weXJpZ2h0IG5vdGljZSwKdGhpcyBsaXN0IG9mIGNvbmRpdGlvbnMgYW5k
IHRoZSBmb2xsb3dpbmcgZGlzY2xhaW1lci4gIFJlZGlzdHJpYnV0aW9ucwpp
biBiaW5hcnk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AgICAgIGV4aXQoLTY2KTsKICAgIH0gZWxzZSBpZiAoICFhcmd2
WzJdICkgewogICAgICAgIGZwcmludGYoc3RkZXJyLCAiXG5FcnJvcjogTmVl
ZCBzb21ldGhpbmcgdG8gcnVuXG4iKTsKICAgICAgICBwcmludF91c2FnZShh
cmd2WzBdKTsKICAgICAgICBleGl0KC02Nik7ICAgICAgICAKICAgIH0KICAg
IC8qICAgCiAgICBmcHJpbnRmKHN0ZGVyciwgIlRpbWVvdXQ9WyVkXSwgZm9y
a2luZz1bJXNdXG4iLCB0aW1lb3V0LCBhcmd2WzJdKTsKICAgICovCiAgICBj
aGlsZF9waWQ9Zm9yaygpOwogICAgCiAgICBpZiAoLTEgPT0gY2hpbGRfcGlk
KSB7CiAgICAgICAgZnByaW50ZihzdGRlcnIsICJFUlJPUjogIEZPUksoKSBG
QUlMRUQhXG4iKTsKICAgICAgICBleGl0ICgtNjYpOwogICAgfQogICAgCiAg
ICBpZiAoY2hpbGRfcGlkKSB7CiAgICAgICAgLyogSSdtIGEgcGFyZW50ISEg
Ki8KICAgICAgICAvKgogICAgICAgICAgICBmcHJpbnRmKHN0ZGVyciwgIlBh
cmVudDogIHNsZWVwaW5nICVkLiAgV2F0Y2hpbmcgY2hpbGQgWyVkXVxuIiwg
dGltZW91dCxjaGlsZF9waWQpOwogICAgICAgICovCiAgICAgICAgc2lnbmFs
KFNJR0FMUk0sICh2b2lkKilvbl9hbGFybSk7CiAgICAgICAgYWxhcm0odGlt
ZW91dCk7CiAgICAgICAgd2FpdHBpZChjaGlsZF9waWQsICZzdGF0dXMsIDAp
OwogICAgICAgIGlmIChXSUZFWElURUQoc3RhdHVzKSkgewogICAgICAgICAg
ICAvKiBjaGlsZCB0ZXJtaW5hdGVkIG9uIGl0J3Mgb3duLCByZXR1cm4gaXQn
cyBzdGF0dXMgKi8KICAgICAgICAgICAgc3RhdHVzPVdFWElUU1RBVFVTKHN0
YXR1cyk7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgLyogZGllZCBm
b3Igc29tZSBleHRlcm5hbCByZWFzb24gKi8KICAgICAgICAgICAgZnByaW50
ZihzdGRlcnIsICJcblBhcmVudDogQ2hpbGQgZGllZCBmb3Igc29tZSB1bmtu
b3duIHJlYXNvbi5cbiIpOwogICAgICAgICAgICBzdGF0dXM9LTY2OwogICAg
ICAgIH0KICAgICAgICBleGl0KHN0YXR1cyk7CiAgICAgICAgCiAgICB9IGVs
c2UgewogICAgICAgIC8qIGNoaWxkICovCiAgICAgICAgLyoKICAgICAgICBm
cHJpbnRmKHN0ZGVyciwgIkNoaWxkOiAgSSdtIGFsaXZlIVxuIik7CiAgICAg
ICAgKi8KICAgICAgICBleGVjdnAoYXJndlsyXSwgJmFyZ3ZbMl0pOwoKICAg
ICAgICBmcHJpbnRmKHN0ZGVyciwgIlxuQ2hpbGQ6ICB1bmtub3duIHByb2Js
ZW06ICBUaW1lcj0lZFxuIix0aW1lb3V0KTsKICAgICAgICBmcHJpbnRmKHN0
ZGVyciwgIlx0ZXhlYzogICclcyIsYXJndlsyXSk7CiAgICAgICAgaT0zOwog
ICAgICAgIHdoaWxlIChpPCBhcmdjKSB7CiAgICAgICAgICAgIGZwcmludGYo
c3RkZXJyLCAiICVzIiwgYXJndltpKytdKTsKICAgICAgICB9CiAgICAgICAg
ZnByaW50ZihzdGRlcnIsIidcblxuIik7CiAgICAgICAgcHJpbnRfdXNhZ2Uo
YXJndlswXSk7CiAgICAgICAgZXhpdCgtNjYpOwogICAgfQp9Cg==

--0-1145242044-1045170710=:72069--