Killing the correct process

Maarten Litmaath maart at cs.vu.nl
Sat Feb 10 13:25:32 AEST 1990


In article <1212 at root44.co.uk>,
	gwc at root.co.uk (Geoff Clare) writes:
)In article <5312 at star.cs.vu.nl> maart at cs.vu.nl (Maarten Litmaath) writes:
)>In article <22332 at adm.BRL.MIL>,
)>	marwood at ncs.dnd.ca (Gordon Marwood) wants to timeout ftp.
)>
)>How about using the following general purpose script?  If that's out of the
)>question, the script might still give you a hint how to solve your problem.
)
)[ extremely complicated script deleted ]
             ^^^^^^^^^^^
There were reasons for that, you know!

)There is a much simpler way than Maarten's.
)
)The basic strategy is:
)
)	(sleep $time; kill $$) & exec "$@"

Here's an example using your timeout command:

	$ timeout -v 0 cat
	Terminated
	$ 
	TIMED OUT "cat"

Sic!  The verbose message appears ASYNCHRONOUSLY!  That's not what I want!
To get a synchronous message I had to have a *child* execute the command
supplied, while the *parent* reports the status.  In fact your strategy is
followed between the backquotes in my script.
Another bug in your script is shown by the following example:

	$ timeout 333 date
	Sat Feb 10 02:56:10 MET 1990
	$ sps
	Ty User     Proc# Command
[stuff deleted]
	p1.maart     1561 sh
	p1. |        1562 sleep 333
[stuff deleted]
	$ 

You leave useless processes hanging around!

)Here is my "timeout" command, using this method with added frills.
)An important feature which Maarten's script lacks, is that mine kills
)the process with SIGTERM, allowing it to clean up.  It only goes for
)SIGKILL if the SIGTERM doesn't do the job.  [...]

How do you determine that the job has finished cleaning up?

Turning back to my script - the only problem with it: the given command
might have created children which keep hanging around after their parent
has died.  This problem could be solved if there were a variant of
`kill(2)' to destroy a complete `process tree'; such a tree is NOT the
same as a process *group*.
Killing the process group (kill -9 -$pid) doesn't work if your shell is
sh, since it doesn't put a `job' into its own process group; you would
destroy unrelated processes as well.
--
  The meek get the earth, Henry the moon, the rest of us have other plans.  |
  Maarten Litmaath @ VU Amsterdam:  maart at cs.vu.nl,  uunet!mcsun!botter!maart



More information about the Comp.unix.questions mailing list