For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > firing an external program and exiting









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author firing an external program and exiting
Ram

2005-08-02, 5:00 pm

I have a situation where a CGI script has to start an independent perl
script and exit without capturing the output or waiting for the exit code.
It doesnt make any practicle sense to me as this perl script takes good 6
hours to run and my CGI script obviously shouldnt keep the user waiting.

exec command doesnt seem to work, nor system command combined with
ampersand(&) in command.

I tried

exec("command");
and also
system("command &");

Niether seemed to work, for the reason which I assume is, since it is a CGI
script and webserver waits until this script exits and then displays the
results to the browser. What was thought to be a simple thing is becoming a
huge problem.

Thanks in advance

Dave Gray

2005-08-02, 5:00 pm

On 8/2/05, Ram <naniid@gmail.com> wrote:
> I have a situation where a CGI script has to start an independent perl
> script and exit without capturing the output or waiting for the exit code=

..
> It doesnt make any practicle sense to me as this perl script takes good 6
> hours to run and my CGI script obviously shouldnt keep the user waiting.
>=20
> exec command doesnt seem to work, nor system command combined with
> ampersand(&) in command.
>=20
> I tried
>=20
> exec("command");
> and also
> system("command &");
>=20
> Niether seemed to work, for the reason which I assume is, since it is a C=

GI
> script and webserver waits until this script exits and then displays the
> results to the browser. What was thought to be a simple thing is becoming=

a
> huge problem.


What does it do exactly? Does it hang? What are the permissions on
this other perl script? Does the user the cgi script is running as
(presumably 'nobody') have permission to execute that script?
Dave Gray

2005-08-02, 5:00 pm

On 8/2/05, Ram <naniid@gmail.com> wrote:
>=20
a[color=darkred]
> CGI
the[color=darkred]
> becoming a=20
>=20
> Yes the user cgi script is running have permissions to execute that
> script.......
> =20
> Infact with the above mentioned methods I am able to execute the script
> very well but my cgi script is waiting until it is getting finished, whic=

h I
> dont want.=20
> =20
> I only want my cgi script to call the external perl script and exit.=20


You're going to need to write a shell script that calls your program like s=
o:

CGI:
system('/path/to/start_program.sh')

SH:
perl /path/to/perl_script.pl &
Ram

2005-08-03, 3:59 am

>
>
> which I
>
> You're going to need to write a shell script that calls your program like
> so:
>
> CGI:
> system('/path/to/start_program.sh')
>
> SH:
> perl /path/to/perl_script.pl &
>
> --
>



Doesnt seem to work :(


Thanks

gariev

2005-08-03, 7:36 am

I guess there's no way to run a program that would run longer than the CGI process itself - the Web server just kill all spawned processes after a while.

Solution:
Your long-running script can be executed from cron daemon (UNIX) or by "at" command (Win2000+) periodically (say, every 5 min).
Your CGI application can leave a flag to the long-running script - if there is a work to do.
The flag can be as simple as an empty file in a certain directory.
The script checks the file and, if found, deletes it and runs. Otherwise, just exits quickly.
Chris

2005-08-03, 10:00 pm

Ram wrote:
> I have a situation where a CGI script has to start an independent perl
> script and exit without capturing the output or waiting for the exit code.
> It doesnt make any practicle sense to me as this perl script takes good 6
> hours to run and my CGI script obviously shouldnt keep the user waiting.
>
> exec command doesnt seem to work, nor system command combined with
> ampersand(&) in command.
>
> I tried
>
> exec("command");
> and also
> system("command &");
>
> Niether seemed to work, for the reason which I assume is, since it is a CGI
> script and webserver waits until this script exits and then displays the
> results to the browser. What was thought to be a simple thing is becoming a
> huge problem.
>
> Thanks in advance
>

Sound like to me that you need to fork. Have a read of perldoc -f fork
and have look at this:

if (my $pid) {
# this is the parent
print "Fork is successfull\n";
}
else {
# this is the child
system("$command") == 0 or die "Child $$ failed to run $command";
exit;
}

HTH
Chris.
Dave Gray

2005-08-03, 10:00 pm

On 8/2/05, Ram <naniid@gmail.com> wrote:
> I have a situation where a CGI script has to start an independent perl
> script and exit without capturing the output or waiting for the exit code=

..
> It doesnt make any practicle sense to me as this perl script takes good 6
> hours to run and my CGI script obviously shouldnt keep the user waiting.
>=20
> exec command doesnt seem to work, nor system command combined with
> ampersand(&) in command.
>=20
> I tried
>=20
> exec("command");
> and also
> system("command &");
>=20
> Niether seemed to work, for the reason which I assume is, since it is a C=

GI
> script and webserver waits until this script exits and then displays the
> results to the browser. What was thought to be a simple thing is becoming=

a
> huge problem.


Let's try this again :) You were most of the way there the first time,
and the shell script thing I suggested is unnecessary.

system("command >/dev/null &"); # works for me

Specifically:

sleep.pl:
#!/usr/bin/perl
$|++;
print STDERR time." start of sleep\n";
sleep(5);
print STDERR time." end of sleep\n";

sleep.cgi:
#!/usr/bin/perl
system('/path/to/sleep.pl >/dev/null &');
print "Content-type: text/html\n\n";
print "<h1>done printing</h1>".time;
Ram

2005-08-03, 10:00 pm

>
> Let's try this again :) You were most of the way there the first time,
> and the shell script thing I suggested is unnecessary.
>
> system("command >/dev/null &"); # works for me
>
> Specifically:
>
> sleep.pl:
> #!/usr/bin/perl
> $|++;
> print STDERR time." start of sleep\n";
> sleep(5);
> print STDERR time." end of sleep\n";
>
> sleep.cgi:
> #!/usr/bin/perl
> system('/path/to/sleep.pl >/dev/null &');
> print "Content-type: text/html\n\n";
> print "<h1>done printing</h1>".time;
>
>
>

Yes this works now......thanks Dave......

You have hell of patience :)


Now since sleep.pl has been invoked by sleep.cgi, sleep.pl will spawn as
child process with cgi script being the parent process. Then
sleep.cgicannot stop until
sleep.pl gets finished.

So effectively, even though the html page is displayed to the user the
sleep.cgi script will not exit and return to webserver.

Is my understanding correct? If it is correct, then how do we make a new
process independent of invoking process?

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com