×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

killing process in tcl

killing process in tcl

killing process in tcl

(OP)
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.

RE: killing process in tcl

This could be done very easily with expect.
I could write a small sample for you if you
are unfamiliar with it.

RE: killing process in tcl

(OP)
what is expect ?
would it be difficult to do ?

RE: killing process in tcl

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.
http://mini.net/tcl/201
http://expect.nist.gov

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.
 
 

RE: killing process in tcl

(OP)
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?

RE: killing process in tcl

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 (http://wiki.tcl.tk), specifically on the "TclX" page, http://mini.net/tcl/207.

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, www.avia-training.com
  866-TCL-HELP (866-825-4357) US Toll free
  415-643-8692 Voice
  415-643-8697 Fax

RE: killing process in tcl

(OP)

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

RE: killing process in tcl

Just a note...

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

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

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, www.avia-training.com
  866-TCL-HELP (866-825-4357) US Toll free
  415-643-8692 Voice
  415-643-8697 Fax

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close