PHP 函数proc_open笔记
来源:互联网 发布:人工智能研究生专业 编辑:程序博客网 时间:2024/05/16 17:22
双管道,多线程执行命令: 使用示例:
test.php:
$descriptorspec=array(
0=>STDIN,
1=>STDOUT,
2=>STDERR
);
$process=proc_open('mysql -u root -p',$descriptorspec,$pipes)
这样 运行php test.php 看见的效果将和终端直接运行mysql命令一模一样。
$descriptorspec说明, 它表述了执行命令的执行文件获得从哪儿获得STDIN以及STDOUT输出到哪儿, STDERR输出到哪儿。
他一般有三种形式, 资源,管道,文件
管道表示:
$descriptorspec=array(
0=>array('pipe','r'),
1=>array('pipe','w'),
2=>array('pipe','w'),
);
管道操作:
if(is_resource($process)){
fwrite($pipes[0],'content');
while($ret=fgets[$pipes[1]){
echo ''.$ret;
}
while($ret=fgets[$pipes[2]){
echo ''.$ret;
}
}
注意事项:
1,读取管道时,有时候不能用stream_get_contents获得内容,必须用fgets, 这时候往往是因为命令执行文件有while死循环,不能结束进程,stream_get_contents是程序结束进程后才能
读取到。 而fgets 每次只读取一行, 要求命令执行文件输出内容需要带换行。 并且有时候需要用while语句循环来读取, 因为fgets每次只读一行 这样保证所有的都读取完了。
2,再读取管道时,往往会堵塞。但是不影响下面程序的执行。 如:
$ret=fgets($pipes[1]);//这里有堵塞。
echo $ret;
fwrite($pipes[0],'content');//但是这里可以执行。
这时候fgets 不能加while循环。
3,stream_set_blocking函数可以设置是否堵塞,但是不知道为什么对管道好像不起作用。
test.php:
$descriptorspec=array(
0=>STDIN,
1=>STDOUT,
2=>STDERR
);
$process=proc_open('mysql -u root -p',$descriptorspec,$pipes)
这样 运行php test.php 看见的效果将和终端直接运行mysql命令一模一样。
$descriptorspec说明, 它表述了执行命令的执行文件获得从哪儿获得STDIN以及STDOUT输出到哪儿, STDERR输出到哪儿。
他一般有三种形式, 资源,管道,文件
管道表示:
$descriptorspec=array(
0=>array('pipe','r'),
1=>array('pipe','w'),
2=>array('pipe','w'),
);
管道操作:
if(is_resource($process)){
fwrite($pipes[0],'content');
while($ret=fgets[$pipes[1]){
echo ''.$ret;
}
while($ret=fgets[$pipes[2]){
echo ''.$ret;
}
}
注意事项:
1,读取管道时,有时候不能用stream_get_contents获得内容,必须用fgets, 这时候往往是因为命令执行文件有while死循环,不能结束进程,stream_get_contents是程序结束进程后才能
读取到。 而fgets 每次只读取一行, 要求命令执行文件输出内容需要带换行。 并且有时候需要用while语句循环来读取, 因为fgets每次只读一行 这样保证所有的都读取完了。
2,再读取管道时,往往会堵塞。但是不影响下面程序的执行。 如:
$ret=fgets($pipes[1]);//这里有堵塞。
echo $ret;
fwrite($pipes[0],'content');//但是这里可以执行。
这时候fgets 不能加while循环。
3,stream_set_blocking函数可以设置是否堵塞,但是不知道为什么对管道好像不起作用。
4,将读取内容加上空字符串后才进行echo输出的目的, 是将STDERR, STDOUT等特殊类型的字符串转换为标准字符串 ,不然有些程序将读取不了这些字符串,比如用 ob_get_clean() 有可能读取不了。
public function __construct($cmd){ $this->pipes = null; $this->descriptors = array( 0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w') ); $this->resource = proc_open($cmd, $this->descriptors, $this->pipes); stream_set_blocking($this->pipes[0], FALSE); stream_set_blocking($this->pipes[1], FALSE); }
$status = proc_get_status($this->resource);
Array( [command] => top [pid] => 7985 [running] => 1 [signaled] => [stopped] => [exitcode] => -1 [termsig] => 0 [stopsig] => 0)
switch($status['exitcode']) { case '255': case '-1': case '1': return false; case '0': // normal end return false; default: // unknown}
public function errors(){ $msg = 'Error:'; while (is_resource($this->pipes[self::PIPE_STDOUT]) && !feof($this->pipes[self::PIPE_STDOUT])) { $msg .= fgets($this->pipes[self::PIPE_STDOUT]); } while (is_resource($this->pipes[self::PIPE_STDERR]) && !feof($this->pipes[self::PIPE_STDERR])) { $msg .= fgets($this->pipes[self::PIPE_STDERR]); } fclose($this->pipes[self::PIPE_STDERR]); proc_close($this->resource); return $msg;}
阅读全文
0 0
- PHP 函数proc_open笔记
- PHP proc_open详解
- PHP Warning: proc_open(): fork failed
- PHP中控制proc_open的执行时间
- PHP - Manual手册 - CXXX. Program Execution Functions程序调用函数 - proc_open执行命令并打开用于输入输出的文件指针
- PHP使用phantomjs导出网址的图片,并使用proc_open输出到浏览器
- php数组函数笔记
- PHP学习笔记-函数
- php函数调用笔记
- PHP 函数笔记,备忘
- PHP笔记(函数)
- PHP函数笔记
- PHP函数笔记
- php函数笔记
- PHP函数学习笔记
- php preg_match_all函数笔记
- php函数笔记
- PHP crc32() 函数笔记
- JavaScript 排序算法(JavaScript sorting algorithms)
- 【PL/SQL】九九乘法口诀表
- 线程的新生到死亡
- 8月14日云栖精选夜读:驱动行业未来20年发展 阿里巴巴AIR视频解说进入倒计时
- 堆棋子与曼哈顿距离
- PHP 函数proc_open笔记
- python解析JSON数据的基本方法
- python的冗余参数
- 1%、2小时、1个亿:贾跃亭在香港的日与夜
- Linux下MySQL5.6的修改字符集编码为UTF8(解决中文乱码问题,亲测可用)
- 动态添加OracleParameter 参数
- 递归与动态规划---数字字符串转换为字母组合的种数
- session-缓存-分布式会话session共享、多节点部署session共享解决方案
- 强密码和弱密码并没有什么区别?NIST密码安全标准更新:不再建议密码要求混合大写字母、字符和数字