[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

proc_open > open a process then let the php script continue

Posted on 2007-10-08
9
Medium Priority
?
3,453 Views
Last Modified: 2013-12-13
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";
?>
0
Comment
Question by:Geoff Millikan
  • 5
  • 4
9 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20037148
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
 

Author Comment

by:Geoff Millikan
ID: 20038071
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
 
LVL 21

Expert Comment

by:nizsmo
ID: 20038842
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 21

Expert Comment

by:nizsmo
ID: 20043617
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
 

Author Comment

by:Geoff Millikan
ID: 20043618
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
 
LVL 21

Expert Comment

by:nizsmo
ID: 20044270
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
 

Author Comment

by:Geoff Millikan
ID: 20105699
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
 

Accepted Solution

by:
Geoff Millikan earned 0 total points
ID: 20106078
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
 
LVL 21

Expert Comment

by:nizsmo
ID: 20106097
Well done on your discovery! Sorry to hear that my solutions did not work, however thanks for sharing your solution!!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

872 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