Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

System calls in perl/cgi 1

Status
Not open for further replies.

madhusn

Programmer
Jul 2, 2001
7
US
Hi All,

In a perl-cgi script, i am trying to execute a system command which runs a perl script in the background.
I'd like to give a message saying the script is running.

The simple cgi script i wrote has the following lines:

===============================================
#!/usr/bin/perl
use CGI qw:)all);


$cmd = "test.pl &";
system($cmd);

print header;
print qq( <HTML>
<TITLE>running Priority Listing</TITLE>
<h3> The report is running ...........</h3>);


print end_html;
===========================================================

This script runs fine. How ever, the message is not displayed until the system command is completed. Even though i've an &quot;&&quot; at the end of the command, it doesnot run the process in the background.

My question is how do i make the script to run the process in the background and give me the message that the report is running, with out waiting for the system command to finish?

Thanks.
Madhu
 
Take a look at the documentation for the fork command.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
This is a difficult issue. The command itself executes immediately. However, the problem here is that the print buffer does not empty to the screen until it has collected all of the script has executed. I'm trying to remember what workaround is available for this. You can try doing an 'eval' on it. That should work, if not, post back here.
HTH. Mike
~~~~
simanek@uiuc.edu
&quot;It's a Swingline!&quot;
~~~~
 
tsdragon, i believe the 'system' command actually has a built in fork. but i could be wrong =) Mike
~~~~
simanek@uiuc.edu
&quot;It's a Swingline!&quot;
~~~~
 
That's true, but the system command waits for the child process to finish, whereas the fork command does not appear to. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
simanek,

'eval' does not work. Please let me know if there's any other solution.

Thanks,
Madhu
 
As a test, I ran SCRIPTA that started SCRIPTB in the background. SCRIPTB printed out a message and then slept for a while. The message was printed out immediately. Normally the print buffer is line buffered. This could be the problem if the message you are printing doesn't have a carriage return. To force a flush on every print, add the line &quot;$|=1;&quot; to both scripts.

 
raider2001,

&quot;$|=1&quot;; didn't help in my case.

i've a SCRIPTA which calls SCRIPTB to run as a background process. I've a message to print from SCRIPTA.
This works fine if i run it from the command line.
How ever, when i try to run the CGI script from the browser, it waits for SCRIPTB to finish completely before it prints whe message in SCRIPTA.

Thanks,
madhu
 
hmmm...

how about opening a pipe and doing something like this:[tt]
open PIPE &quot;perl script.pl |&quot;;
print &quot;script running\n&quot;;
while (<PIPE>)
{
print;
#or whatever else you want done with the data
}
[/tt]
that might work for you. even better would be to make yourself a mod_perl handler, but don't worry about that - that's at least a week of studying before you can get it to work perfectly. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
stillflame,

it doesn't seem to work.
Could you please explain what PIPE symbol does here?
i tried looking up for this information in books, i couldn't find much.

Thanks,
Madhu
 
it's not a special symbol, it's just a normal filehandle, well, a streamhandle in this case, as the &quot;file&quot; is really a command which ends in the '|' character, known in *nix as the pipe character. when you open a &quot;pipe&quot; like this, putting the character at the beginning means that you can print to that streamhandle and that data will be passed to the STDIN of the running command (as is used for using sendmail in a perl script (see the FAQ section here for more on that)). with the pipe character at the end of the command (as it is in the code i posted), the filehandle can be read from and the data received will be that which was printed to STDOUT by the command (normal 'print' statements in perl print to STDOUT).
i'm not sure why it's not working for you. could you be more specific as to what's not happening, or post the code you used to implement it?
oh, and one thing i forgot to mention is to 'close' the handle after you're done with it (after the while loop). not absolutely necessary, but still very important.[tt]
close PIPE;
[/tt] &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top