PHPKonf: Istanbul PHP Conference 2017


(PHP 4 >= 4.2.0, PHP 5, PHP 7)

pcntl_exec Führt ein angegebenes Programm im aktuellen Prozessraum aus


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

pcntl_exec() führt das Programm path mit den Parametern args aus. path muss der Pfad zur binären ausführbaren Datei oder einem Skript mit einem gültigen auf eine ausführbare Datei zeigenden Pfad in der Shebang (zum Beispiel #!/usr/local/bin/perl) als erste Zeile sein. Suchen Sie in der execve(2) man Seite Ihres Systems nach weiteren Informationen.

args ist ein Array von Parameterstrings, die dem Programm übergeben werden.

envs ist ein Array von Strings, die dem Programm als Umgebungsvariablen übergeben werden. Das Array ist im Format Name => Wert, wobei der Schlüssel der Name der Umgebungsvariable und der Wert der Inhalt dieser Variable ist.

pcntl_exec() gibt bei Fehlern FALSE zurück und kehrt bei Erfolg nicht mehr zurück.

add a note add a note

User Contributed Notes 6 notes

agodong at verizon dot net
10 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
//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);

I use this as a part of socket program so that a program can be run under different user from remote location.
fahadsadah at gmail dot com dot please dot remove dot this
8 years ago
Just a note regarding what rbemrose at vgmusic dot com said:
Once the executed process ends, control returns to the webserver process.
rbemrose at vgmusic dot com
8 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.
eric kilfoil
10 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

// 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.
michael dot ferre at mobileway dot com
14 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);

//This code is very simple it can be ameliorate ;)
maxime at diametrick dot com
3 years 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.

  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');

To Top