龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

PHP启动windows应用程序、执行bat批处理、执行cmd命令的方法(exec、system函数详解)(2)

时间:2014-10-22 02:09来源:网络整理 作者:网络 点击:
分享到:
复制代码 代码如下: php set_time_limit(0); define('ROOT_PATH', dirname(__FILE__)); include ROOT_PATH . '/include/global.func.php'; $cmdTest = 'ps -ef | grep magent'; $lastLine = system($cmdTest

复制代码 代码如下:

<?php
set_time_limit(0);
define('ROOT_PATH', dirname(__FILE__));

include ROOT_PATH . '/include/global.func.php';

$cmdTest = 'ps -ef | grep magent';

$lastLine = system($cmdTest, $retVal);

write_log('$lastLine');
write_log($lastLine);

write_log('$retVal');
write_log($retVal);
?>

输出:

复制代码 代码如下:

++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:28:52
$lastLine
++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:28:52
root      5375  5373  0 16:28 pts/1    00:00:00 grep magent
++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:28:52
$retVal
++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:28:52
0

【exec】

复制代码 代码如下:

<?php
set_time_limit(0);
define('ROOT_PATH', dirname(__FILE__));

include ROOT_PATH . '/include/global.func.php';

$cmdTest = 'ps -ef | grep magent';

$lastLine = exec($cmdTest, $output, $retVal);

write_log('$lastLine');
write_log($lastLine);

write_log('$output');
write_log($output);

write_log('$retVal');
write_log($retVal);
?>

输出:

复制代码 代码如下:

++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:25:00
$lastLine
++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:25:00
root      5360  5358  0 16:25 pts/1    00:00:00 grep magent
++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:25:00
$output
++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:25:00
Array
(
    [0] => root      2838     1  0 15:39 ?        00:00:00 magent -u root -n 51200 -l 192.168.137.100 -p 12001 -s 192.168.137.100:11211 -b 192.168.137.100:11212
    [1] => root      5358  5356  0 16:25 pts/1    00:00:00 sh -c ps -ef | grep magent
    [2] => root      5360  5358  0 16:25 pts/1    00:00:00 grep magent
)

++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:25:00
$retVal
++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:25:00
0

结论:

如果需要详细的输出结果,那就用exec()吧!我一般用exec()执行外部命令!

参考:

http://php.net/manual/zh/function.system.php
http://php.net/manual/zh/function.exec.php

精彩图集

赞助商链接