[net.unix-wizards] Timed self-destruct

bet@ecsvax.UUCP (Bennett E. Todd III) (12/19/85)

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

bet@ecsvax.UUCP (Bennett E. Todd III) (01/07/86)

Well, I got a few helpful responses. I appreciate everyone's forbearance
with such ugly code.

I got suggestions for good tricks to play with adb, for ways to refine
the program I wrote, and other such tips. However, the real kicker comes from:
	Stephen Samuel
	{ihnp4,ubc-visi}!alberta!uofa-mts!stephen_samuel

who gets the prize for cleverest hack of the bunch:

( sleep 120 ; kill -9 $$ ) &
exec $*

His explanation is succinct:

"What it does is have the kill delete the primary process, which is then
replaced with the real intended program. Note that this also results in
a mimimum of extra procsses and NO temporary files."

Hat's off. I guess this illustrates the basic nature of UNIX; there is
generally only one correct way to do a thing, and many many wrong ways,
most of which will occur to you before the right one.

-Bennett
-- 

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