Re: Killing a running process
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: Killing a running process

From: Glenn Jackman <xx087@freenet.carleton.ca>
Date: Fri Mar 03 2006 - 21:17:39 CET

At 2006-03-03 02:45PM, al-s <stosh259@hotmail.com> wrote:
> OK. This is what I tried and I get an error: can't read "pids": no
> such variable. The log reads the above plus: wile executing
> "kill-proc $pids"
> (command bound to event)
>
> Code is as follows:
>
> proc RunCmd {command log} {
> if [catch {open "|$command |& cat"} fd] {

useless use of cat. you want
      if {[catch {open "| $command 2>@stdin"} fd]} {

> $log insert end $fd\n
> } else {
> set pids [pid $fd]
> bind $log <Control-Key-c> {kill-proc $pids}

Don't use braces here (ref: http://wiki.tcl.tk/9330) -- in that case,
Tcl is looking for the global variable pids. You want a list so your
local $pids is substituted:

         bind $log <Control-c> [list kill-proc $pids]

As an aside, I've found that I have to bind both lower case c and upper
case C. Is there a way to make bind be case insensitive?

[...]
> proc kill-proc {pids} {
> catch {exec kill $pids}
> }

That won't work with multiple pids: Suppose $pids is {1 2 3 4}. The
kill command will receive a single argument "1 2 3 4" instead of the 4
process id's you intend. This proc is better written as:

    proc kill-proc {pids {signal TERM}} {
        if {[catch [linsert $pids 0 exec kill -$signal] result] != 0} {
            puts "problem killing pids '$pids': $result"
        }
    }

-- 
Glenn Jackman
Ulterior Designer
Received on Sun Apr 30 02:23:05 2006