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

Hi Monks,
I don't know if I am asking in the right place. I need to write a program which triggers a cronjob/script whenever a text file has been created. The cronjob/script will copy the file name to an index file. But I am not getting, how to inform my cronjob/script that a new file had been created. I think it has to be informed by the OS. But I may be wrong.
Again, I don't know if I am asking at the right place and hope you all will show some light to this problem.
Thanks
Navzit

Replies are listed 'Best First'.
Re: Trigger a cronjob in perl
by Fletch (Bishop) on May 27, 2009 at 18:34 UTC

    Cron runs cronjobs at periodic intervals (that's it's job); there's not really any connection between filesystem events and it. You certainly could have a program (that cron runs periodically) which looks at the contents of a directory (using readdir and friends) and compares that to a list kept in a file, but the cron-ness has nothing to do with it really (comparing apples and badgers).

    The other alternative would be to use something like Sys::Gamin or the like in a persistent program which runs constantly until the OS notifies it that a directory has changed, but again that's got nothing to do with cron.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Trigger a cronjob in perl
by toolic (Bishop) on May 27, 2009 at 18:41 UTC
    This also has nothing to do with cron, but File::Monitor may be able to help you.
Re: Trigger a cronjob in perl
by ig (Vicar) on May 27, 2009 at 21:40 UTC

    I would first try to write the program so that it executes the script at the appropriate time. This is generally simple and minimizes latency. I would consider alternatives only after identifying a reason to do otherwise.

      There is a machine I work on which has an arrangement like this. (Although as has been pointed out, the cron script is not 'triggered'.)

      The cron shell script runs each 5 minutes and just looks in an 'inbound' directory to see if there is anything there. If so it calls a second script (this one in Perl) which does some stuff with the file and then moves the file to a 'done' directory or to a 'failed' dir if it did not like the inbound file data . Simple and works.


      No time to do clever stuff with signature.
Re: Trigger a cronjob in perl
by navzit (Initiate) on May 29, 2009 at 18:02 UTC
    Hi Monks,
    Thanks for the suggestions . Yeah , I don't want something to be run periodically so Cronjob is not the right way to do it. I will look into the the 'File: Monitor' and see if it helps. My aim is to avoid looking for updated files periodically.The file itself should inform my script that it has been created so do something.
    Thanks again
    Navzit