Timed self-destruct (for debugging programs that hang)

Bennett E. Todd III bet at ecsvax.UUCP
Thu Dec 19 13:58:43 AEST 1985


I was hacking on MicroEmacs this evening (trying to bring it up under
4.2) and kept hanging my terminal (MicroEmacs was reading RAW, but
wasn't stripping parity; commands weren't recognized). So I started
doing approximately the following:

	stty tostop		# background jobs hang on tty output
	microem &		# it gives PID, then the critter hangs
	stty -tostop		# dasn't let this one hang!
	sleep 120;kill -9 pid & # where pid is filled in from above
	%microem		# bring it to the foreground

This worked well enough, but was a significant bother -- manually
copying pids is a drag. So I tried to automate the process -- I wanted
to be able to type

	timeout microem

and have 120 seconds to play before it got killed off. I wrote the
following. PLEASE note:

	I *know* it's ugly
	I *know* it shouldn't blat out all the inappropriate prompts
	I *realize* that it is unbearably inefficient

It took me an hour or two to get it working at all (stubborn I am) and
another couple of hours to give up on trying to clean it up. Any
suggestions for how to straighten this sucker out and make it less
grungy are welcome; in the interim, ugly as it is, it works. Hope you
enjoy this:

csh -i $* <<'EOF'
set tmpfile=/tmp/timeout$$
stty tostop >/dev/tty
$* </dev/tty >/dev/tty &
jobs -l >$tmpfile
set pid=`grep $1 $tmpfile | awk '{print $3}'`
rm $tmpfile
stty -tostop >/dev/tty
sleep 120 ; kill -9 $pid &
%$1
EOF

Like I said, about all that can be said for it is that it works, and I
can't say that for any of my efforts to clean it up. If you can, please
let me know how.

One more quick thought -- note that the grep over the output of jobs -l
could potentially match other jobs you have around, particularly if you
are debugging a program with an unusually short name.

-Bennett
-- 

"Hypocrisy is the vaseline of social intercourse." (Who said that?)

Bennett Todd -- Duke Computation Center, Durham, NC 27706-7756; (919) 684-3695
UUCP: ...{decvax,seismo,philabs,ihnp4,akgua}!mcnc!ecsvax!duccpc!bet



More information about the Comp.unix.wizards mailing list