Hello all,

we're trying to write a simple pair of client/server sockets in order to facilitate the simultaneous starting and stopping of scripts on several machines simultaneously. The client part works just fine - binds to port, prints a string (the name of a script to execute, such as "startAll>8-30-2001.log" or "killTomcat") to the server filehandle, and closes the socket. The server socket (see below) *sort* of works - the first string it gets from the client gets executed, but then the process hangs around and (presumeably because the server socket never gets an explicit "i'm all done now" message) gives this: insite   26839  0.0  0.0     0    0 ?        Z    14:55   0:00 [listener <defunct>]

As you can see below, command execution is being done via backticks; the docs seem to make it clear that this should do what we want it to do, i.e. let the server socket daemon keep running. We thought about increasing the number of possible queued connections, or spawning a new processes for every client connection, and while this does in fact work, it leaves a lot of defunct processes lying around, which just seems like a stupendously bad idea (not to mention the ugliness of said hack). Any ideas?

ned

#!/usr/bin/perl #listener.pl use Socket; use POSIX; # Forking and letting parent die... $pid = fork; exit if $pid; die "Couldn't fork: $!" unless defined($pid); # Disassociate from controlling term... POSIX::setsid() || die "Can't start a new session: $!"; # Trap fatal signals and set a graceful exit flag... $time_to_die = 0; sub signal_handler { $time_to_die = 1; } $SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&signal_handler; until ($time_to_die) { $local_port = 1234; # Setting up server for listening... my $proto = getprotobyname('tcp'); socket(SERVER, PF_INET, SOCK_STREAM, $proto) || die "Can't create sock +et: $!\n"; setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) || die "Can't setsocko +pt $!\n"; bind(SERVER, sockaddr_in($local_port, INADDR_ANY)) || die "Can't bind +to socket: $!\n"; listen(SERVER,SOMAXCONN) || die "Can't listen to socket: $1"; while ($paddr = accept(CLIENT, SERVER)){ $command = (<CLIENT>); '$command'; } close(SERVER); }

In reply to silly defunct processes... by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.