Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

interrupting a daemon

by qq (Hermit)
on Oct 23, 2004 at 15:39 UTC ( #401850=perlquestion: print w/replies, xml ) Need Help??
qq has asked for the wisdom of the Perl Monks concerning the following question:

I've got several daemons (using Proc::Daemon) that scrape web pages and insert the info into a mysql database.

If a daemon gets killed (which they do a lot while testing) do I need to explicity close the mysql connection in a signal handler? Is there any other cleanup I should be thinking about?

I'd appreciate pointers to good reading material on daemons. I've read the Camel material, and I've picked up pieces here and there. But they tend to focus just on how simple they are. Are they?

Thank you, qq

Replies are listed 'Best First'.
Re: interrupting a daemon
by Zaxo (Archbishop) on Oct 23, 2004 at 19:54 UTC

    The DBI connection handle's destruction will close the connection, so there is no need to arrange for a signal handler to do it. If a statement handle may still be around with unread data, you can avoid a warning by calling finish() on it. I would do that in a DESTROY or END sub rather than in the signal handler.

    I gave a blow-by blow account of what makes a daemon daemonic in Re: Creating a perl daemon.

    After Compline,

      The DBI connection handle's destruction will should close the connection.

      Unfortunately it is easy to show it does not always work that way out in the real world. Even the DBI docs suggest you explicitly disconnect. I can site personal experience with fairly current versions of Perl/DBI/DBD::Mysql that will let you accumulate open connections until you run out.

Re: interrupting a daemon
by lhoward (Vicar) on Oct 23, 2004 at 16:00 UTC
    MySQL (and a lot of other back ends) will probably do the right thing if you just let the connection die, but its always best to put in a handler and close things up nicely. L

      By default MySQL will *do the right thing* very slowly. It will actually close an inactive connection after 8 hours. Given that by default there are only 100 connections available it is quite easy to use them all up if you do not do a clean disconnect. You set the values in my.cnf BTW.

      [mysqld] set-variable=wait_timeout=28800 set-variable=max_connections=100

      Cleanup is simple. All you need is an END block, ideally placed immediately after the connect (so you know it is there).

      my $dbh = DBI->connect...... END{ $dbh->disconnect() if $dbh }



Re: interrupting a daemon
by Your Mother (Bishop) on Oct 23, 2004 at 23:57 UTC

    There is also Network Programming With Perl (sample chapters and all code from book:, which discusses how to do HUPs and such on Daemons to give them the opportunity to do whatever you need them to do depending on the signals they get. He shows how to write a really nice base class for them which implements Sys::Syslog logging. I really think this book should be on every perl hacker's desk next to the Camel and Conway's OOP.

Re: interrupting a daemon
by qq (Hermit) on Oct 25, 2004 at 14:12 UTC

    Thanks for all the helpful replies. I've added an END block, and checked the max connections. qq

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://401850]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2018-07-17 09:46 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (363 votes). Check out past polls.