I can see a race condition that might occur when you start to process the file, and then the background process starts to process the file at the same time. So some form of locking/semaphore is required.

To only allow one process running, you could create a simple semaphore file, like inprogress.txt, under the data directory, and your generate_report.pl script will look for the semaphore file first before processing, with something like below:

# check for semaphore file my $data_dir = "C:/data"; my $semaphore = "$data_dir/inprocess.txt"; die "another process is in progress" if -f $semaphore; # create the semaphore file open SEMAPHORE, ">$semaphore" or die "Can not create semaphore"; close SEMAPHORE; # do the processing... # delete the semaphore file at the end # you could put this bit in the END block. unlink $semaphore;
And also your generate_report.pl script should first generate/write the report to a temp file, and then rename/copy it to the cached final report at the end of processing. So while the report generation is still underway, users could still retrieve the most recently generated report.

You could let the CGI script to spawn the generate_report.pl script as a background process, when the user clicks the 'Kickoff Processing' button, and then display a message like -

Please be patient while the report is being generated. This process may take more than 2 hours to complete. You can still refer to the most recent report <here> while the new report is being created.

In reply to Re: Launch background perl script from CGI by Roger
in thread Launch background perl script from CGI by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.