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

Dear Monks, i have written a script that uses Daemon::Simple to daemonize and DBIx::DWIW for some very basic database operations. In the main loop, the script looks for some information from a logfile than updates a MySQL Database with that information and eventually sends an eMail, thatn it starts over again. Now the problem: The script runs fine when startet with daemonizing outcommented, as soon as i remove the # in front of Daemon::Simple::init(...) the script hangs at the first Execute(); here's some of the code:
use strict; use Daemon::Simple; use Log; use DBIx::DWIW; use Mail::Sender; use Data::Dumper; if ($ARGV[0] eq 'start') { start(); } elsif ($ARGV[0] eq 'stop') { stop(); } #### S T O P PROGRAM #### sub stop { $Log->log_it ("info", "$prog_name exiting..."); ## Initialize Daemon ## my $homedir = '/'; my $pidfile = "/var/run/$prog_name.$service_name.pid"; my $command = 'stop'; Daemon::Simple::init($command,$homedir,$pidfile); } #### S T A R T PROGRAM #### sub start { $Log->log_it ("info", "$prog_name started."); $DB = DBIx::DWIW->Connect (@dbpref) || $Log->error("No Database-Con +nection!!"); ## Initialize Daemon ## my $homedir = '/'; my $pidfile = "/var/run/$prog_name.$service_name.pid"; my $command = 'start'; # Daemon::Simple::init($command,$homedir,$pidfile); loop(); } sub loop { ## Daemon Loop ## while (1) { $Log->debug('loop started...'); @tasks_processed = (); my @entries = get_new_entries; # $Log->debug('entries: '.Dumper (@entries)); # prepare SQL my $sthErrMsg = $DB->Prepare ("UPDATE tasks SET task_errormsg = +?, task_done = '1' WHERE task_id = ?"); for (@entries) { # doing some stuff with @entries # .... # update DB $sthErrMsg->Execute($status, $task_id) or $Log->error("DB Erro +r: $@"); # here it hangs forever when Daemonized # some other stuff } } # end while } # end loop ()
Is Daemon::Simple conflicting with DBI?

Replies are listed 'Best First'.
Re: DBI(x) conflicting with Daemon::Simple?
by moritz (Cardinal) on Feb 23, 2009 at 15:53 UTC
    Daemon::Simple uses fork, and that requires care when handling database handles. The DBI method also has something to say about fork, search for the InactiveDestroy option.
      thanks! your hint lead me to just create the $dbh AFTER daemonizing, as my script doesn't need a Database connection in the parent process. Now it works fine
      Now I have a new problem: the script is supposed to run 24/7, but after about 24 h it hangs again in the DBI Execute. After restarting the script it runs fine again. Any hints?