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

Hello,
I allready used supersearch to search on daemons and daemonize. I found the Proc::Daemon module. But I have no clue how to use it :)

Let me sketch my problem:

I have simple perl-script that queries a database, makes XML and sends it with LWP to another server who sends back the XML answer in the content. That same script parses this and puts that data back in the database.I want this script to run in the background. Like it run ones every 3 seconds, but if it still runs after 3 secons it should wait until it's session is finished and then start another one. So it should only run one instance at a time.



--
My opinions may have changed,
but not the fact that I am right

Replies are listed 'Best First'.
Re: Daemons???
by Zaxo (Archbishop) on Oct 26, 2001 at 13:12 UTC

    Here is the basis for a general daemon. Add code at the end to do what you want. This follows the Stevens rules.

    #!/usr/bin/perl -w use strict; use POSIX qw( setsid ); my $debug = 1; my $logfile = q(.testlog); # Season to taste my @fh_unused = (\*STDIN, \*STDOUT); open \*STDERR, ">> $ENV{'HOME'}/$logfile"; select((select(\*STDERR), $| = 1)[0]); { # Daemon Rule 1) Fork and exit the parent. my $ppid = $$; my $pid = fork and exit 0; ! defined $pid and die "No Fork: ", $!; while (kill 0, $ppid) { select undef, undef, undef, .001; }; } # Daemon Rule 2) become session leader, pg leader, no term my $session_id = POSIX::setsid(); # Daemon Rule 3) cd to / chdir '/' or die "Could not cd to rootfs", $!; # Daemon Rule 4) set file creation mask to 0 my $oldmask = umask 00; # Daemon Rule 5) Close unneeded file handles close $_ or die $! for @fh_unused; # Your code here exit 0;
    If you open communication channels before the fork, the child will inherit their handles.

    After Compline,
    Zaxo

Re: Daemons???
by kwoff (Friar) on Oct 27, 2001 at 04:09 UTC
    There a section in `perldoc perlipc`, search for "daemon".
Re: Daemons???
by Fletch (Bishop) on Oct 26, 2001 at 21:06 UTC

    You might also look into POE. You can set things up so that the kernel posts events back to you every however many seconds. That event could kick off the parsing and what not.