kill_all.tcl

GPS Here is a little script I wrote to kill a fork bomb:

 proc list.processes {} {
        set p [open {|ps -ax}]
        set data [read $p]
        close $p
        return $data        
 }
 proc main {argc argv} {
        if {1 != $argc} {
                return -code error {please specify one argument}
        }
        set killThis [lindex $argv 0]

        while 1 {
                set ps [list.processes]
                set pList [split $ps \n]
                set killedAny 0
                foreach p $pList {
                        puts P:$p
                        if {[regexp "(\[0-9\]+).*[set killThis]\[ \t\]*\$" $p -> pid]} {
                                puts PID:$pid
                                catch {exec kill -9 $pid}
                                set killedAny 1
                        }
                }
                if {!$killedAny} return
        }
 }
 main $::argc $::argv

Usage: tclsh8.4 kill_all.tcl someProcessName