?
Solved

Running a perl script in background

Posted on 2003-03-31
5
Medium Priority
?
171 Views
Last Modified: 2010-03-05
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){
#child
`/home/scripts/uptime_pgm /home/scripts/addresses/all_NetworkIP`; #script to be run
}
else {
#parent
     
print<<END_of_Start;
Content-type: text/html

<HTML>
   <HEAD>
   <TITLE>Report Generator</TITLE>
   </HEAD>
   
   <BODY>
   <P>Processing your request View the results page after 15 minutes</P>
   </BODY>
</HTML>
END_of_Start
exit;
     
}

The problem is the browser doesnot close, it closes only after the child has finished.
Why is this code not working?
0
Comment
Question by:manad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 9

Expert Comment

by:owenli27
ID: 8240842
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())
{
    Print_HTML_Msg();
    exit(0);
}
`/home/scripts/uptime_pgm /home/scripts/addresses/all_NetworkIP'
0
 
LVL 1

Accepted Solution

by:
bobbisson earned 320 total points
ID: 8243878
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:


--
daemon.pl

#!/perl

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 };

sleep

--

--
script.pl

#!/perl

use POSIX qw(:signal_h);

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

# print HTML
--
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9707901
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
 
LVL 1

Expert Comment

by:bobbisson
ID: 9720479
I agree :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question