newbie01.perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I've done some "basic" reasonable Perl coding and now there is a requirement to have the Perl scripts that is currently running as cron job to be invoke via a Perl daemon?

Can anyone please refer me to any tutorial if any that discusses something similar to what am wanting to achieve?

Thanks in advance.

Replies are listed 'Best First'.
Re: Perl daemon ?
by keszler (Priest) on Nov 20, 2009 at 10:34 UTC
Re: Perl daemon ?
by codeacrobat (Chaplain) on Nov 20, 2009 at 07:04 UTC
    Maybe Schedule::Cron is what you are looking for.

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});
Re: Perl daemon ?
by baxy77bax (Deacon) on Nov 20, 2009 at 20:40 UTC
    hi,

    to make a simple daemon just de-attach the called script with this peace of code:

    die "Cannot continue :$!" unless(defined(my $kid = fork())); unless ($kid){ system("my scheduler"); exit; }
    and now when you demonize your script then you can with a simple while loop schedule all scripts set in some dir like:
    in my scheduler : ----------------- while(1){ # predefined diractory opendir(DIR,"some dir") || die "$!"; foreach my $sc (readdir(DIR)){ next if ($sc=~/\./g); system($sc); } sleep 3600; # of whatever time you need }
    and that is more-or-less what you need to set up your first crone-like application. (just remember to set the absolute path with readdir -> it just captures the names of scripts in opened folder). of course don't forget to chmod to 555 to all files in defined dir (either through perl or manually).

    good luck !

      Hi,

      I think this is more or less what I wanted. I found some example that uses fork as well but not sure whether that will actually make any difference.

      Also found a Daemon.pm, but not to sure on how to use it.

Re: Perl daemon ?
by rcaputo (Chaplain) on Nov 20, 2009 at 06:35 UTC

      Hi Ron,

      Nope, am not wanting to write a cron daemon. I think the response from baxy77bax is what am looking for unless you have another suggestion.

      Initially, am looking at scheduling the several scripts to run via cron but then our UNIX SA suggested that daemon'izing it is better.

      He doesn't suggest cron 'coz he said sometimes cron may not be running on some of the servers or the cron daemon itself may die which had happened in some occassions. I suppose the while (1) may be the trick, hopefully it does not become a rogue or runaway task or become defunct.

      I've also found a Daemon.pm, not sure if that is useful in anyc case.

        Consider using daemontools (see also thedjbway). They are essentially while(1) on steroids, completely eliminating any need to have daemon management, configuration, or logging code in your code. With daemontools, a simple daemon does not need more than 10 lines of shell code.

        And by the way: If a daemon like cron crashes and is not restarted automatically, either the machine's hardware or the OS setup is severely broken.

        What you you need the background process for? For some routine task that has to be done every N time-units? That's clearly a job for cron. No need to re-invent wheels, just wake up your lazy admin. Or do you need some service that responds to requests that may happen at any time? That's a job for a dedicated daemon, preferably under control of daemontools.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)