Just a note regarding what rbemrose at vgmusic dot com said:
Once the executed process ends, control returns to the webserver process.
pcntl_exec
(PHP 4 >= 4.2.0, PHP 5)
pcntl_exec — 現在のプロセス空間で指定したプログラムを実行する
説明
void pcntl_exec
( string $path
[, array $args
[, array $envs
]] )
指定した引数でプログラムを実行します。
パラメータ
- path
-
path は、バイナリ実行ファイルへのパスか、 あるいは有効な実行ファイルを指す shebang (例: #!/usr/local/bin/perl) が一行目に存在するスクリプトへのパスである必要があります。 詳細な情報については、システムの man ページで execve(2) を参照ください。
- args
-
args は、プログラムに渡す引数文字列の配列です。
- envs
-
envs は、プログラムに渡す環境変数の配列です。 この配列は name => value のような形式で、key が環境変数名・value が その値となります。
返り値
エラー時に FALSE を返し、成功時には何も返しません。
pcntl_exec
fahadsadah at gmail dot com dot please dot remove dot this
28-Jun-2008 07:44
28-Jun-2008 07:44
rbemrose at vgmusic dot com
06-Feb-2008 02:44
06-Feb-2008 02:44
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.
agodong at verizon dot net
07-Dec-2006 10:47
07-Dec-2006 10:47
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.
eric kilfoil
24-Oct-2006 10:01
24-Oct-2006 10:01
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.
michael dot ferre at mobileway dot com
20-Nov-2002 03:28
20-Nov-2002 03:28
//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 ;)
michael dot ferre at mobileway dot com
19-Nov-2002 04:19
19-Nov-2002 04:19
//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
//Michal F. php developer .
