downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

pcntl_fork> <pcntl_errno
[edit] Last updated: Fri, 14 Jun 2013

view this page in

pcntl_exec

(PHP 4 >= 4.2.0, PHP 5)

pcntl_execExécute le programme indiqué dans l'espace courant de processus

Description

void pcntl_exec ( string $path [, array $args [, array $envs ]] )

Exécute le programme indiqué dans l'espace courant de processus.

Liste de paramètres

path

path doit être le chemin vers un binaire exécutable ou un script avec un chemin valide pointant vers un exécutable à la première ligne (par exemple, #!/usr/local/bin/perl). Voir les pages d'aide de votre système concernant execve(2) pour plus d'informations.

args

args est un tableau d'arguments sous la forme de chaînes de caractères passées au programme.

envs

envs est un tableau de chaînes de caractères qui sont passées au programme comme variables d'environnement. Le tableau est de la forme nom => valeur, la clé est le nom de la variable d'environnement et la valeur est la valeur de cette variable.

Valeurs de retour

Retourne FALSE si une erreur survient et ne retourne rien en cas de succès.



pcntl_fork> <pcntl_errno
[edit] Last updated: Fri, 14 Jun 2013
 
add a note add a note User Contributed Notes pcntl_exec - [8 notes]
up
2
fahadsadah at gmail dot com dot please dot remove dot this
4 years ago
Just a note regarding what rbemrose at vgmusic dot com said:
Once the executed process ends, control returns to the webserver process.
up
1
rbemrose at vgmusic dot com
5 years ago
As a side note, if I'm reading the comments below correctly, you should not run this if you're using a PHP webserver module, as it will replace the webserver's process with whatever process you're telling it to run.
up
1
agodong at verizon dot net
6 years ago
Some people might find it useful to run other program using the same process as a different user. This is very usefull if the script is running under root. Here is a simple code to achieve that under *nix PHP CLI:

#!/usr/bin/php -q
<?php
//Enter run-as user below (argument needed to be passed when the script is called), otherwise it will run as the caller user process.

$username = $_SERVER['argv'][1];

$user = posix_getpwnam($username);
posix_setuid($user['uid']);
posix_setgid($user['gid']);
pcntl_exec('/path/to/cmd');
?>

I use this as a part of socket program so that a program can be run under different user from remote location.
up
1
michael dot ferre at mobileway dot com
10 years ago
//To complete my last note
//If you use some object in your php code
//You will have some problem if you do a exit after include the
//child scripts
//You must use posix_kill() like that :

$CHILD_PID = pcntl_fork();
if($CHILD_PID == 0)
{   
  include ($script_path);
  posix_kill(getmypid(),9);
}

//This code is very simple it can be ameliorate ;)
up
0
maxime at diametrick dot com
1 month ago
An alternative for Windows environment is to use the COM object. With this, you can start child process with Apache, or php.exe as parent process. Very useful for CLI or console PHC-Win applications.

if(!function_exists('pcntl_exec')){
  function pcntl_exec($path,$args=array()){
    if(is_string($args)) $args = array($args);
    if(count($args)) $path = '"'.$path.'"';
    $shell = new COM('WScript.Shell');
    $shell->run($path.(count($args) ? ' '.implode(' ',$args) : ''),0,true);
  }
}

Very useful too when you want to use ansicon. Instead of make a .bat file with ansicon -p and php .... (somefile).php, in your .php file you can put this on top of your code:

pcntl_exec('ansicon -p');

BINGO!
up
0
eric kilfoil
6 years ago
The pcntl_exec() function works exactly like the standard (unix-style) exec() function.  It differs from the regular PHP exec() function in that the process calling the pcntl_exec() is replaced with the process that gets called.  This is the ideal method for creating children.  In a simple example (that does no error checking):

switch (pcntl_fork()) {
  case 0:
    $cmd = "/path/to/command";
    $args = array("arg1", "arg2");
    pcntl_exec($cmd, $args);
    // the child will only reach this point on exec failure,
    // because execution shifts to the pcntl_exec()ed command
    exit(0);
  default:
    break;
}

// parent continues
echo "I am the parent";

--

since this is not being executed through a shell, you must provide the exact path from the filesystem root.  Look at the execve() man page for more information.
up
-1
trevorpearce at gmail dot com
1 year ago
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";
}
?>
up
-1
michael dot ferre at mobileway dot com
10 years ago
//If you are in php version between 4.1 and 4.2
//When you use pcntl_fork and after you want use
//pcntl_exec, you can because pcntl_exec isn't used before
//php 4.2 so let me show a solution :

$CHILD_PID = pcntl_fork();
if($CHILD_PID == 0)
{   
   include ($script_path);
   exit;
}

//$script_path is the  *.php page which you want lauch like
// a child .

//I don't know why php coder hadn't add  pcntl_exec in the
//same time that other function

//Michaël F. php developer .

 
show source | credits | stats | sitemap | contact | advertising | mirror sites