Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Stopping an HTTP::Server::Simple server

by textual (Novice)
on Jan 26, 2011 at 14:19 UTC ( #884353=note: print w/ replies, xml ) Need Help??

Comment on Re^3: Stopping an HTTP::Server::Simple server
Replies are listed 'Best First'.
Re^4: Stopping an HTTP::Server::Simple server
by Corion (Pope) on Jan 26, 2011 at 14:48 UTC

    This is surprising, as HTTP::Server::Simple should then die in the parent with Can't fork: ..., which you should have seen:

    sub background { my $self = shift; my $child = fork; croak "Can't fork: $!" unless defined($child); return $child if $child; srand(); # after a fork, we need to reset the random seed # or we'll get the same numbers in both branches if ( $^O !~ /MSWin32/ ) { require POSIX; POSIX::setsid() or croak "Can't start a new session: $!"; } $self->run(@_); # should never return exit; # just to be sure }

    So, either you're looking at a webserver that is not the webserver you launched, or the $pid is not where you store the information.

    If you're trying to kill the child from within the child, $pid will also not be defined there (see fork). To kill yourself, use $$ as the pid.

      Another possibility is that the op is trying to launch the webserver in one process invocation, and then kill it in another. In which case $pid will have to be written to/from a file.
        Thanks a lot for your replies.

        Sorry, my previous answer was ambiguous: $pid is undefined in the handle_request () method of MyWebServer, but it is correctly set to something like "-1470" in test.pl.

        In fact, writing this in test.pl effectively stops the server immediately:
        use strict; use warnings; use MyWebServer; my $server = MyWebServer->new(8080); my $pid = $server->background(); kill 9, $pid;

        But in order to execute this instruction in response to some HTTP request, (I think) I need to call it from within a handler in MyWebServer.

        I find that the following:
        kill 9, $$;
        effectively stops the server if it is added anywhere in MyWebServer.pm's "body" (e.g. at the first line), but not if it is added in the handle_request() method.

        I've also tried tilly's suggestion to have test.pl write the PID in a file, then have handle_request() read this file. It does succeed at passing the PID, and I do not get the "Can't kill a non-numeric process ID" message anymore, however the call to kill does not stop the server in this case.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://884353]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (12)
As of 2015-07-29 02:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls