php exec 进程,PHP 在当前进程空间执行指定程序

用户评论:

maxime at diametrick dot com (2013-05-02 06:20:21)

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!

trevorpearce at gmail dot com (2012-01-22 18:36:07)

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"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

{//foreach ($tasks as $key => $v){switch ($pid=pcntl_fork()) {

case -1:// @faildie('Fork failed');

break;

case0:// @child: Include() misbehaving code hereprint"FORK: Child #{$x}\\n";

echo"child after fork:",getmypid(),PHP_EOL;pcntl_exec($programexe,$programvars);// generate_fatal_error(); // Undefined functionbreak;

default:// @parentprint"FORK:$xParent\\n";

echo"parent after fork:",getmypid(),PHP_EOL;// pcntl_waitpid($pid, $status);break;

}

print"Done! :^)\\n\\n";

}?>

fahadsadah at gmail dot com dot please dot remove dot this (2008-06-28 07:44:49)

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 (2008-02-06 14:44:18)

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 (2006-12-07 10:47:19)

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

I use this as a part of socket program so that a program can be run under different user from remote location.

eric kilfoil (2006-10-24 10:01:12)

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 (2002-11-20 03:28:23)

//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 (2002-11-19 04:19:21)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值