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

Hey all, I'm writing a program to convert some data into a spreadsheet and then email the results back to the user. The machine doing the work is different than the one supplying the data. I need to run the program every few seconds to check for new data and the crontab is a little limiting in that it will only go to the minute. What is the best way to run the program persistently? I know that there are some "daemon" modules at there. Is this the right path? Do I just need some simple coding within the program? Any help would be appreciated. Thanks. Dalin
I've often wondered what it would be like to have something to say.

Replies are listed 'Best First'.
(jeffa) Re: Persistence
by jeffa (Bishop) on Oct 08, 2001 at 20:23 UTC
    Personally, sounds to me like an event-driven model would be better - why roll your own if you have HTTPD at your disposal? But . . . if you _have_ to write a daemon, here are a couple of resources:

    jeffa

Re: Persistence
by George_Sherston (Vicar) on Oct 08, 2001 at 21:01 UTC
    Glad you asked this question, as it's something I'm going to have to grapple with in a few days. I'll watch this thread with interest to see if the answer to my question comes up :)

    At the moment, how I'm planning to approach it (subject to revision by sibling monks) is
    while (1) { open OFF, "off.txt"; last if <OFF>; # all the code goes here sleep 5; } print "you turned me off!";
    off.txt is a blank file. So then my script runs and runs and runs until I put something into off.txt, at which point the script breaks out of the loop.

    As I say, that's how I'd do it... but I'd also ask how to do it before I did it that way. As you have.

    § George Sherston
      I prefer the file test operators to opening the file and reading from it to stop a daemon. It is probably faster and it is easyer to understand.
      until (-e "off.txt") { #change -e to -s to behave like George's code #code here sleep 5; }
Re: Best way to make a script run persistently (daemon)
by entropy (Sexton) on Oct 09, 2001 at 01:12 UTC
    The easiest way to make a script run in the background is just by putting an & after the command (if you are using a standard unix shell). If you want to make a program that backgrounds on its own, you should fork and then use the "setsid" command.
    use POSIX qw(setsid); exit if fork(); # parent dies setsid; # make this a new process group ... rest of your program ...
Re: Best way to make a script run persistently (daemon)
by zeitgheist (Novice) on Oct 09, 2001 at 07:11 UTC
    Dalin,
    There was a recent contest on Webreference about UNIX daemons in Perl. The contest is HERE. The tutorial is HERE. The sources were made available somewhere on those URLs.
    HTH

    cheers,
    zeitgheist
Re: Best way to make a script run persistently (daemon)
by Starky (Chaplain) on Oct 09, 2001 at 00:32 UTC
    One additional mention that I don't often see in the literature in case you have some backend action going on: I have found that often after a fork I have to reconnect all my database handles. For some reason they frequently don't seem to survive the fork.

    Hope this helps :-)

      If you read up on the InactiveDestroy database handle attribute, you'd know that forked processes should not share a database handle, and some databases won't even pass a db handle across a fork. If a child process needs a db handle, then you should connect in the child process.