Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Process Control Functions : pcntl_exec

pcntl_exec

Executes specified program in current process space (PHP 4 >= 4.2.0, PHP 5)
void pcntl_exec ( string path [, array args [, array envs]] )

Executes the program with the given arguments.

Parameters

path

path must be the path to a binary executable or a script with a valid path pointing to an executable in the shebang ( #!/usr/local/bin/perl for example) as the first line. See your system's man execve(2) page for additional information.

args

args is an array of argument strings passed to the program.

envs

envs is an array of strings which are passed as environment to the program. The array is in the format of name => value, the key being the name of the environmental variable and the value being the value of that variable.

Return Values

Returns FALSE on error and does not return on success.

Code Examples / Notes » pcntl_exec

eric kilfoil

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.


agodong

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.


michael dot ferre

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


Change Language


Follow Navioo On Twitter
pcntl_alarm
pcntl_exec
pcntl_fork
pcntl_getpriority
pcntl_setpriority
pcntl_signal
pcntl_wait
pcntl_waitpid
pcntl_wexitstatus
pcntl_wifexited
pcntl_wifsignaled
pcntl_wifstopped
pcntl_wstopsig
pcntl_wtermsig
eXTReMe Tracker