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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP calling Perl but NOT waiting for Perl to finish??

Status
Not open for further replies.

WMAdam

Programmer
Jun 5, 2002
14
0
0
US
Hello,
What if you want a PHP web page to launch a Perl script but keep it in the background- don't have PHP wait around for its output. I have a PHP page that is supposed to launch a perl script that sends a bunch of email and does database work and I don't want the page waiting around for perl to finish.
Here is the line I use to launch the perl script. (The & is supposed to spawn its own process, yet PHP insists on waiting for the script to complete!)

$cmd = "/usr/bin/perl $perl_path/process_email.pl &> $perl_path/error";
system($cmd);


Any ideas?
thanks!
 
You can't launch the perl script directly.

Have PHP run a script that closes STDIN, STDOUT, and STDERR then itself runs the script you want to run in the background.

If you want to run a perl script called "foo.pl", then have PHP run a script called "launcher.pl", which consists of:

#! /usr/bin/perl

close (STDIN);
close (STDOUT);
close (STDERR);

system ("foo.pl &");


PHP will return immediately after launcher.pl ends, but foo.pl will continue running.
 
Looks great- thanks a lot for the advice!
 
I feel I must give credit where credit is due.

A smart fellow by the name of Michael Kohn figured out how to do it using a c-language executable as the launcher. I just found his post on the user comments to the online PHP manual and adapted his idea to perl.

You can find Mr. Kohn's elaboration on the idea at:
 
Very clever idea, nonetheless. I very much appreciate the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top