Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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??

in reply to Re^2: Stopping an HTTP::Server::Simple server
in thread Stopping an HTTP::Server::Simple server

It's actually not defined.
  • 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

        In fact, writing this in 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'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 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://884353]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2018-06-21 02:00 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.