Running a perl script in background

I am designing a web interface to generate some reports.
The perl script that generates these reports takes about 15 minutes to run.
I donot want the user to wait that long so I am trying to run it as a background.
This is what I have implemented-

if (!defined($pid=fork)) {
die "Unable to fork!";
elsif (! $pid){
`/home/scripts/uptime_pgm /home/scripts/addresses/all_NetworkIP`; #script to be run
else {
Content-type: text/html

   <TITLE>Report Generator</TITLE>
   <P>Processing your request View the results page after 15 minutes</P>

The problem is the browser doesnot close, it closes only after the child has finished.
Why is this code not working?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Weiping DuSenior Web DeveloperCommented:
Not sure this will work, how about use child process to print out HTML msg and use parent process to generate report. such as:
if($myspid = fork())
`/home/scripts/uptime_pgm /home/scripts/addresses/all_NetworkIP'
from perlform manpage (perl 5.6.1):

exit() always exits just the executing pseudo-process, after automatically wait()-ing for any outstanding child pseudo-processes.  Note that this means that the process as a whole will not exit unless all running pseudo-processes have exited.

A way to mark a pseudo-processes as running detached from their parent (so that the parent would not have to wait() for them if it doesn't want to) will be provided in future.

Possible solution that comes to my mind is to create a daemon process that will run your script on a special signal, like this:



use POSIX qw(setsid);
use POSIX qw(:signal_h);

sub _sig_HUP {
# do necessary handling

open(FILE,'>/path/to/lock') or die "Can't create lock file: $!\n";
print FILE $$;
close FILE;

chdir '/'                 || die "Can't chdir to /: $!\n";

open STDIN,  '/dev/null'  || die "Can't read /dev/null: $!\n";
open STDOUT, '>/dev/null' || die "Can't write to /dev/null: $!\n";

defined(my $pid = fork)   || die "Can't fork: $!\n";
exit 0 if $pid;

setsid || fatal $self("Can't start a new session: $!");

$SIG{HUP}  = sub { &_sig_HUP };





use POSIX qw(:signal_h);

open FILE,'/path/to/lock';
my $daemon = <FILE>;
close FILE;
# get daemon pid somehow
kill SIGHUP,$daemon;

# print HTML

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nothing has happened on this question in over 7 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by bobbisson.

Please post any comments here within the next seven days.


EE Cleanup Volunteer
I agree :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.