Tanuj
Tanuj

Reputation: 318

How do I check for spawn_id that's alive? (TCL)

I spawn a telnet process to a host. I send a command, expect something in return. This goes on for a while. But somewhere in between this interaction, the connection to the host is lost mysteriously and my script dies while trying to "send" something to the spawned (now dead) telnet process. I'd like to write a procedure that takes the spawn id and the command to be sent as arguments. I'd like to check if the spawn id exists (i.e., the connection between the program and the host exists) before I "send" the command. Otherwise, I'd like to exit. Something like this:

proc Send {cmd sid} {
if { $sid is not dead yet } { ;## don't know how to do this
part
send -i $sid "$cmd\r"
} else {
puts "channel id: $sid does not exist anymore. Exiting"
exit
}
}

Upvotes: 4

Views: 6557

Answers (2)

evil otto
evil otto

Reputation: 10582

Rather than checking if the spawned process is still alive, you could catch the error that send raises when sending to a dead process:

proc Send {cmd sid} {
    if {[catch {send -i $sid "$cmd\r"} err]} {
        puts "error sending to $sid: $err"
        exit
    }
}

Upvotes: 2

Hai Vu
Hai Vu

Reputation: 40723

I ran into this problem before and used the Mac/Linux ps command to do that:

if {[catch {exec ps $pid} std_out] == 0} { 
    puts "Alive"
} else {
    puts "It's dead, Jim"
}

If you are using Windows, I heard that the tlist.exe command does something similar, but I don't have a Windows machine to test it out.

Upvotes: 1

Related Questions