proc_open > open a process then let the php script continue

I want my main PHP script to kick off another subscript and continue running.  I don't care about the subscript and I don't need any feedback from it.  I just wnat to run it.   I cannot use exec() since that waits for the subscript to finish.  I cannot use pcntl_fork() because I'm on Windows.  I tried to use proc_open() as in the below example but it's still waiting for the subscript to complete before continuing the main script execution.  How do I do this?
<?php
$descriptorspec = array(
       0 => array("pipe", "r"),   // stdin
       1 => array("pipe", "w"),  // stdout
       2 => array("pipe", "w")   // stderr
);
$process = proc_open('php c:\webroot\watchdog\run_the_subscript.php', $descriptorspec, $pipes);
echo "keep running main script";
?>
Geoff MillikanAsked:
Who is Participating?
 
Geoff MillikanAuthor Commented:
Eureka!!  Use the following:

exec("C:\PHP\bgrun C:\PHP\php -q c:\webroot\watchdog\MasterCard_Determine_Cost.php>nul",$dummy_var);

But first copy the bgrun.exe file into your php directory (c:\php).  Get the program at the following link:
http://www.jukkis.net/bgrun/

More on this AWESOME little "bgrun.exe" program on Google at the link here:
http://www.google.com/search?ie=UTF-8&oe=UTF-8&q=bgrun.exe
http://www.thescripts.com/forum/thread1522.html
0
 
nizsmoDeveloperCommented:
Below site shows information on what you are probably looking for, which is often referred to as "multi-threading":

http://www.alternateinterior.com/2007/05/multi-threading-strategies-in-php.html

Please let me know how you get on. Unfortunately PHP has no easy way of doing this, as you will probably realize after reading the above site.
0
 
Geoff MillikanAuthor Commented:
I think the script at the link above is making it more compliacted than I need.  The script above is a whole process management which returns the results and the buffer. I don;t need all that, I just want to run a separate script and forget about it while the main script keeps on running.  Seems like such a simple thing but maybe it not?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
nizsmoDeveloperCommented:
Understood, before I post the link I did think it was a little complicated but as the article said there's no way of multi-threading in PHP unfortunately. I posted the link as a reference maybe you would find something useful and make your above example work by simplifying the code provided in the link.

I will hunt around see what else I can find.
0
 
nizsmoDeveloperCommented:
If you are not interested in the process after it starts, you can ask the
OS to start up a new process, even if it's only web accessible:

for ($x=0; $x<count($devices); $x++) {
$url="http://localhost/snmp_grab.php?dev=" . $device[$x];
$cmd="curl '$url'"; // or {"php -q /docroot/snmp_grab " . $device[$x]}
// or lynx, or w3m, or ...
`echo "$cmd" | at now`;
}

Please let me know how you go.
0
 
Geoff MillikanAuthor Commented:
I can make the script open the subscript as a webpage too like this but again, running it with exec makes the main script hang until execution is completed.  
exec('http://localhost/run_the_subscript.php');

I know this is possible because the manual on the PHP function passthru() at the link below says, "Note: If you start a program using this function and want to leave it running in the background, you have to make sure that the output of that program is redirected to a file or some other output stream or else PHP will hang until the execution of the program ends."  I just cannot find an example that shows how to "redirect the output to a file."

http://us3.php.net/manual/en/function.passthru.php
0
 
nizsmoDeveloperCommented:
Hi, this shows an example of maybe preventing the passthru to hang and keep script execution going? I don't have an example myself so couldn't try:

function traf(){
  passthru( './traf.sh' );
  echo "<br />\n";
  flush(); // keeps it flowing to the browser...
  sleep( 1 );
}

Basically the important thing is the flush() command, disregarding the sleep which is only there because it is part of the example.
0
 
Geoff MillikanAuthor Commented:
nizsmo,

Your solution posted on 10.09.2007 at 12:08PM PDT, ID: 20043617 doesn't work.  In the example below, it doesn't run the "myprogram.php" script.  
`echo "php -q c:\webroot\watchdog\myprogram.php" | at now`;

Your solution posted on 10.09.2007 at 01:26PM PDT and shown below doesn't work either, like all the others, it waits for the myprogram.php to finish running for continuing on down the script.

function traf(){
  passthru( 'php -q c:\webroot\watchdog\myprogram.php);
  echo "<br />\n";
  flush(); // keeps it flowing to the browser...
  sleep( 1 );
}
traf();
echo "Is the script still executing?";
0
 
nizsmoDeveloperCommented:
Well done on your discovery! Sorry to hear that my solutions did not work, however thanks for sharing your solution!!
0
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.

All Courses

From novice to tech pro — start learning today.