Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

killing process in tcl

Status
Not open for further replies.

jlynch1

Technical User
Dec 20, 2001
100
0
0
IE
I am using the following line of code to execute the ping utility from a tcl/tk application.

if {[catch {open "|ping -c $num_packets -s $packetsize -i $packetinterval $destaddress |& cat"} pipe]} {

When ping is running and someone sends an interupt ie. ctrl+c
The statistics are generated at the very bottom

eg.
--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% loss, time 1005ms
rtt min/avg/max/mdev = 0.031/0.042/0.054/0.013 ms

I want to be able to send an interrupt to the process while it is running and generate the statistics. A kill -9 on the pid doesnt generate the statistics.

I would appreciate if anyone could help me out here.
 
This could be done very easily with expect.
I could write a small sample for you if you
are unfamiliar with it.
 
what is expect ?
would it be difficult to do ?
 
No, very trivial.
Expect is a set of tcl that has been around for a
very long time. There are ports of it's functionality
to perl and python.

It basically automates interactive programs.
If I have a ping process I spawn from an expect
script I can take interpret and guide that process,
catch and handle signals destined for it,parse the output
of the ping process and basically do whatever I want.
 
I have tcl code written already to process the output of the ping process.
Would it have been much easier to do in expect ?

Is there any function in tcl to send signal 15 to the process given its process id?
 
In the standard Tcl distribution, there is no way to send a signal to another process. Without using an extension, you'll have to exec the Unix kill command, providing the appropriate PID and signal type. If you're able and willing to use extensions, TclX provides access to many Unix system calls as Tcl commands, including a kill command. You can find out more information about TclX on the Tcl'ers Wiki ( specifically on the "TclX" page,
For either approach, you'll need to know the PID of the process you want to kill. In case you weren't aware of how to get that, Tcl's pid command can provide it to you. From the pid reference page:

pid ?fileId?

DESCRIPTION

If the fileId argument is given then it should normally refer to a process pipeline created with the open command. In this case the pid command will return a list whose elements are the process identifiers of all the processes in the pipeline, in order.
- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 

exec kill -s SIGINT [lindex [pid $pipe] 1]

will do the job for me.

Didnt know you could specify the signal types in the kill command.

thanks for the help
 
Just a note...

You'll need to kill the ping process, not the cat process. That means you'll need to execute:

[tt][ignore]exec kill -s SIGINT [lindex [pid $pipe] [/ignore]0[ignore]][/ignore][/tt]

Also, when killing pipes, remember to use a catch when you close your side of the pipe. You'll often get an error condition in these cases to signal the abnormal termination of the pipeline process. - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top