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

Hello dearest Perl Monks!

I seek for your wisdom.

I have a perl script, that when executed it writes a FILE. The process is a bit longer, as the file is written by getting the data from another database and then comput together.

The thing is, that I need to run this script every morning, but often occurs that people add new things to the database, and then my file is not updated (I am writting an HTML file with PERL), until I update it manually.

So I seek for your wisdom, if you can tell me how to make my PERL script run automaticly, lets say, every 2 hours?

There are some topics about this talking about Daemon (yes I am using Linux), but most links didnt open.

BR, YouthMonk
  • Comment on How to make PERL Script run Automaticly?

Replies are listed 'Best First'.
Re: How to make Perl script run automatically?
by Discipulus (Canon) on Jul 04, 2014 at 07:36 UTC
    seems a duty for cron more than for Perl.

    If you dont like this simple solution you can write a service, even if i think you dont need it.
    The simplest one is something like:
    perl -e 'while(1){do_something;eventually_sleep_sometime;}' &
    Id est: run in background a program that run infinitely. But consider you have to instruct your OS to run also at boot, you need to manage signals, you have to keep running only one instance of it...

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Thanks for this! Yes, cron seems the simplest, although, I do not have some rights to access cron in here. SO I have to find alternatives. I will test your idea
        Speak to your friendly sysadmin and convince him to add your script to the crontab list.

        The alternative is that you run a script for a looooong period, most of the time doing nothing and that will use some resources that perhaps may be better used elsewhere.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
        If so start reading this doc and this post too.

        HtH
        L*
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: How to make PERL Script run Automaticly?
by Anonymous Monk on Jul 04, 2014 at 07:29 UTC
      The sleep module seems interesting, I have to check on it, if it's for my purposes.

      Unfourtanetly I cannot access cron in here, it would be easiest indeed.

Re: How to make PERL Script run Automaticly?
by David92 (Sexton) on Jul 04, 2014 at 07:56 UTC
    So I was trying out some things and I came out with following:

    for(;;){ sleep 7200; somesub(); }
    Is this interpreted as I want it to be - does this run infinitly, then every 2 hours it executes the 'somebus()' routine?
      Yes, it will run approximately every 2 hours, although if somesub() take some time to execute, you have to realize that the sleep 7200 will start only after the completion of the sub so that the start time of one start compared to the previous will be offset by 2 hours + the duration of the sub. So, in effect, il will run every 2 hours + sub execution duration. This might or might not matter for you.

      A cron job would not suffer from this defect, may be you can ask your sysadmin. One workaround is to have the subroutine to launch the process as a background process, so that the subroutine would take only a couple of second to execute.

      Update: fixed two typos in the word duration.

        good point Laurent, thanks! For now, it is not of that matter, because I timed it that the script needs 10minutes to run. Later on, this will come into matter, as the script will grow larger.

        Just to clearify it for myself: So if I run the above code once in Linux .. it will continue running until I close the Linux client, right?

Re: How to make PERL Script run Automaticly?
by pvaldes (Chaplain) on Jul 04, 2014 at 19:59 UTC

    I'll suggest to consider also other points of view.

    You need to make a move only when some user changes something in the database. I this don't occurs really very often, you can just to ask the DB to warn YOU (and maybe to run your perl script) if, and only if, something is changed.

    So maybe what you need is to put a species of "system(perl myscript)" command IN the SQL language of your database choice (look at "triggers" and "create procedure" for your DB)... or maybe not. How often is your DB changed, and what permits you have as DB user, DB admin... etc, is the key here.