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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |