note 107220 modified in function.pcntl-exec by danbrown
| From: | danbrown@php.net | Date: | Thu, 10 May 2012 21:37:57 +0000 |
| Subject: | note 107220 modified in function.pcntl-exec by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187916@lists.php.net to get a copy of this message | ||
I spent a lot of time trying to figure this powerful option out so I didn't get error 2 or
error 8. The documentation says it can execute a binary file and here is a good example that works
that I found; <?php print
"Before\\n";
pcntl_exec("/usr/bin/uptime"); print
"After\\n"; ?> However I
wanted it to run another php program and after wasting a lot of time I finally got
it. <?php $tasks2=array('/var/www/example.php'); pcntl_exec("/usr/bin/php",$tasks2); ?> The
php script has to be in an array and you execute the binary php
file I'll be adding this to launching a child and run it
seperately using this
function <?php function
launchchild($programexe,$programvars) { //foreach ($tasks as $key
=> $v){ switch ($pid = pcntl_fork()) { case
-1: // @fail die('Fork
failed'); break; case
0: // @child: Include() misbehaving code here
print "FORK: Child #{$x} \\n"; 		 echo "child
after fork:", getmypid(), PHP_EOL; 		
pcntl_exec($programexe,$programvars); // generate_fatal_error(); //
Undefined function break;
default: // @parent print "FORK: $x
Parent\\n"; 		 echo "parent after fork:",
getmypid(), PHP_EOL; // pcntl_waitpid($pid, $status);
break; } print "Done!
:^)\\n\\n"; } ?>
--was--
I spent a lot of time trying to figure this powerful option out so I didn't get error 2 or
error 8. The documentation says it can execute a binary file and here is a good example that works
that I found;
print "Before\n";
pcntl_exec("/usr/bin/uptime");
print "After\n";
However I wanted it to run another php program and after wasting a lot of time I finally got it.
$tasks2=array('/var/www/example.php');
pcntl_exec("/usr/bin/php",$tasks2);
The php script has to be in an array and you execute the binary php file
I'll be adding this to launching a child and run it seperately using this function
function launchchild($programexe,$programvars)
{
//foreach ($tasks as $key => $v){
switch ($pid = pcntl_fork()) {
case -1:
// @fail
die('Fork failed');
break;
case 0:
// @child: Include() misbehaving code here
print "FORK: Child #{$x} \n";
echo "child after fork:", getmypid(), PHP_EOL;
pcntl_exec($programexe,$programvars);
// generate_fatal_error(); // Undefined function
break;
default:
// @parent
print "FORK: $x Parent\n";
echo "parent after fork:", getmypid(), PHP_EOL;
// pcntl_waitpid($pid, $status);
break;
}
print "Done! :^)\n\n";
}
http://php.net/manual/en/function.pcntl-exec.php