Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Logging out

by Dr. Mu (Hermit)
on Oct 26, 2001 at 21:47 UTC ( #121674=perlquestion: print w/replies, xml ) Need Help??

Dr. Mu has asked for the wisdom of the Perl Monks concerning the following question:

I'm building a headless data acquisition box (RH Linux 7.1), which is controlled over a network by a Windows laptop. Both the acquisition program (server) and control program (client) are written in Perl. The Linux box's video output is used to monitor the incoming data in real time. I'm using Tk under Gnome to display the data. I'm able to login via Net::Telnet from the client program, start Gnome, and get the server program running. From there, communication with the server program takes place using SOAP::Lite. What's got me stuck at this point is the reverse process -- logging out. Does anyone know how to either:

1. Send the telnet logout command via an open Net::Telnet connection? (Once Gnome is started via startx, telnet blocks, waiting for the X session to finish, so using $connection->cmd is not possible. Also, breaking the telnet connection doesn't force a logout -- nor do I want it to.)


2. Have the Perl script running on the server force an exit/logout sequence?

Many thanks! This has had me stumped for several days now, and I've exhausted the resources I'm familiar with.

Replies are listed 'Best First'.
Re: Logging out
by Dr. Mu (Hermit) on Oct 27, 2001 at 10:50 UTC

    Well, the following snippet of code is pretty ugly, but it seems to work. It is executed by the server program in response to a command from the client via SOAP.

    my @ps = `ps -ef`; my @pid = (split(/ +/, (grep(/login -- mrc/, @ps))[0]))[1]; my @kill; while (1) { last unless @pid = grep {$_ > 1} @pid; push @kill, @pid; my $pidlist = join('|', @pid); last unless @pid = grep(/\s+[\d]+\s+($pidlist) /, @ps); @pid = map {(split(/\s+/, $_))[1]} @pid; } kill 'TERM', @kill
    What this does is get a list of all running processes, finds one named "login -- mrc", then goes through the list looking for all its children, children's children, etc. Once it's done this, it kills them all. Ugh.
      As I'm a great proponent of implementing in Perl rather than relying on external programs, I came up with the following more generic code which is a bit cleaner in that it doesn't depend on the external execution of ps :

      sub killchd ($;$) { use Proc::ProcessTable; my $sig = ($_[1] =~ /^\-?\d+$/) ? $_[1] : 0; my $proc = Proc::ProcessTable->new; my %fields = map { $_ => 1 } $proc->fields; return undef unless exists $fields{'ppid'}; foreach (@{$proc->table}) { kill $sig, $_->pid if ($_->ppid == $_[0]); }; kill $sig, $_[0]; };

      This subroutine takes two arguments, the parent process ID and the numeric signal to pass to the processes (which would be 9 if you wanted to issue a -TERM). Using Proc::Process you could find the process ID of the process login -- mrc with something similar to the following :

      my $proc = Proc::ProcessTable->new; my @ps = map { $_->pid if ($_->cmndline =~ /login -- mrc/) } @{$proc-> +table}; &killchd($_, 9) foreach @ps;


      Ooohhh, Rob no beer function well without!

        Yes, I like that much better. Thanks!
Re: Logging out
by kwoff (Friar) on Oct 27, 2001 at 03:56 UTC
    Is it possible to have another telnet session (forked), then `killall xinit` to kill the server. I'm not sure I fully understand the requirements though.
      Unfortunately not. I'm using vncserver with Gnome for administrative work. If I kill all the xinits, the one serving VNC goes, too.

      I did try this anyway, though, just to see what would happen. Gnome and my Perl script were left running as zombies. So when I tried manually logging out of Gnome, I couldn't.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://121674]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2023-12-08 02:04 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (34 votes). Check out past polls.