tsk1979 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, Yet another Net::Telnet query. I am writing a script which does telnet to multiple machines, and runs programs. That part is complete, now I am writing the sighandler routine. what I want to do is when ^C gets called etc., the script should close all the open telnet sessions and due. For doing that I would need the process id of the telnet? All the telnets are being done from one machine. I could do a global pgrep telnets and xargs kill -9 but that would kill all telnet sessions, not just the ones started by my script. I just want to kill those started by the script. Any pointers?

Replies are listed 'Best First'.
Re: Process id in Net::Telnet
by zer (Deacon) on Mar 23, 2006 at 07:18 UTC
    where is the ^C being read? STDIN or some telnet output?
    $ok = $obj->close; #will close your telnet according to the object

    The objects arent really recorded as pid's because they are not being called as a seperate process.

      STDIN, id from where the script was run. Suppose I do something like $telnet->cmd("Run some program"). the program is running and the user presses ^C on the term where the script was started. I want all telnet sessions to close. Currently the telnet sessions are started inside a subroutine with my $telnet->new.... Suppose the script has 20 telnet sessions started with private my $telnet. In my ^C handling code I want to make sure all these sessions close. I was wondering if I kill the script with ^C will it automatically kill the telnet sessions or will some zombie sessions remain?
Re: Process id in Net::Telnet
by codeacrobat (Chaplain) on Mar 23, 2006 at 07:46 UTC
    perldoc perlipc says:
    Sending a signal to a negative process ID means that you send the signal to the entire Unix process-group.
    This code sends a hang-up signal to all processes in the current process group (and sets $SIG{HUP} to IGNORE so it doesn't kill itself):
    { local $SIG{HUP} = 'IGNORE'; kill HUP => -$$; # snazzy writing of: kill('HUP', -$$) }
    Use this hint as a pointer. I haven't done much IPC stuff lately.
    I am assuming that your script does not fork of other non-telnet processes that should not be killed.
Re: Process id in Net::Telnet
by zer (Deacon) on Mar 23, 2006 at 07:29 UTC
    ya when you kill the script the telnets die
      Yup, the telnets will die, but I have spawned multiple telnet threads. In those threads I am running cmds like $telnet->cmd("make"); So when that telnet dies does the make also die or it keeps running? If it keeps running I need a way to kill all those makes when ^C is pressed.