sri has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Next to an Apache/mod_perl combo I'm running a small daemon for continuous number crunching.

Both Apache and the daemon are very related.

The daemon is currently started and stopped through its own "daemonctl" script.

Now I'm searching for a solution to get the small daemon started and stoppen through Apache.
Starting is quite easy through "startup.pl" and a few other ways.
But to get it stopped is not that easy!

The only solution that came to my mind was to check the pid file of the Apache whenever the daemon reaches the end of its main loop. (not very elegant)
A wrapper script for both "apachectl" and "daemonctl" is ugly too.

I checked a lot of documentations/books/tutorials but found no direct way like a special cleanup/shutdown handler or the like.
Think i saw a note somewhere that Apache itself has no such handler.

Any suggestions for a clean solution?

(No matter which version of apache/mod_perl)
  • Comment on starting/stopping a daemon from Apache/mod_perl?

Replies are listed 'Best First'.
Re: starting/stopping a daemon from Apache/mod_perl?
by cees (Curate) on Jul 13, 2003 at 02:32 UTC

    Since you are using mod_perl, perhaps a dummy object with a DESTROY method in your startup.pl file will work. Stick all of this in at the end of your startup.pl file:

    our $shutdown = My::ShutdownHandler->new(); package My::ShutdownHandler; sub new { return bless {}, shift; } sub DESTROY { #Do your thing here };

    It will get executed twice, since Apache does a restart immediately after starting, but you should be able to test for that using $Apache::Server::Starting or $Apache::Server::ReStarting. I am running mod_perl2 on the box I tested this on, and those 2 variables appear to be depricated, so I can't test that part of it. But the above seemed to execute only on shutdown of the server...

    If this does work for you, it will require some heavy testing... I didn't test to see what would happen on a killall -USR1 httpd, or something sinister like killall -9 httpd.

    - Cees

      Thanks!

      Looks like it's the solution I searched!
Re: starting/stopping a daemon from Apache/mod_perl?
by Zaxo (Archbishop) on Jul 12, 2003 at 20:58 UTC

    Have you tried, system '/path/to/daemonctl', 'stop'; or whatever other argument you may need for daemonctl?

    After Compline,
    Zaxo

      It doesn't matter how to kill the daemon, system() or kill() or something else...

      The problem is to stop the daemon out of the Apache.

      If I type "apachectl stop" or "kill <APACHEPID>" I want to trigger an event in the Apache (system, kill...) that stops my daemon.
        That is a question that has nothing to do with Perl or perl, and all with Apache. You will have more luck asking this question in forum where this kind of questions are on-topic.

        Abigail

Re: starting/stopping a daemon from Apache/mod_perl?
by bobn (Chaplain) on Jul 12, 2003 at 22:22 UTC

    I see no really clean way to do this from mod_perl. The closest I can see would be to install a PerlChildExitHandler - as noted in Chapter 7 of the "eagle" book, "Writing Apache Modules with Perl and C" - but this runs each time a child apache process ends, so it would still have to check that it was the last child process left up, (because shutdown is not the only reason a process goes down), then kill your other daemon. And I'm not sure what a "child" process means, if anything, in the Win32 environment.

    Update: If the child processes are put down in parallel during shutdown, (eg. they're all sent a signal as members of a process group) there could be a race, where your PerlChildExitHandler still sees other processes up when in fact they are all coming down. Your less-desired solution of having the other daemon check for the Apache process each time it loops may be the safest way to do this.

    --Bob Niederman, http://bob-n.com
      Yes, PerlChildExitHandler would be a solution if i could make sure that the child gets killed because of a server shutdown.