PHP:OSX命令行上的pcntl_fork()

时间:2015-06-25 04:44:37

标签: php macos command-line-interface pcntl

我有一台运行OSX Yosemite的Macbook Pro。我试图在命令行上使用pcntl_fork()运行一个简单的PHP脚本(命令:php pcntl.php):

<?php
    $pid = pcntl_fork();

    switch($pid) {
        case -1:
            print "Could not fork!\n";
            exit;
        case 0:
            print "In child!\n";
            break;
        default:
            print "In parent!\n";
    }
?>

答案如下:

  

致命错误:在左侧调用未定义的函数pcntl_fork()   /Users/grant/Desktop/test/pcntl.php第2行

如果您正在运行mamp,我已经看过很多文章向您展示如何安装pcntl,但如果您只是使用终端,那么您将如何安装pcntl?如果在命令行中无法做到这一点,PHP中是否有类似的功能可以使用?

1 个答案:

答案 0 :(得分:6)

In case of native CLI you should proceed the same way as in case of MAMP. The only difference is that you should add support to native php and not to MAMP's one. Yosemite's php doesn't have pcntl support. Following command returns no output: $ php -i | grep pcntl Verify your php version: $ php -v PHP 5.5.20 (cli) (built: Feb 25 2015 23:30:53) Download and build pcntl module: $ wget http://php.net/distributions/php-5.5.20.tar.xz $ tar xf php-5.5.20.tar.xz $ cd php-5.5.20 $ cd ext/pcntl/ $ phpize $ ./configure $ make Copy module to extensions folder: $ sudo cp modules/pcntl.so /usr/lib/php/extensions/no-debug-non-zts-20121212/ Edit php.ini configuration file: $ sudo vi /etc/php.ini And add extension=pcntl.so line in Dynamic Extensions section, e.g.: ;;;;;;;;;;;;;;;;;;;;;; ; Dynamic Extensions ; ;;;;;;;;;;;;;;;;;;;;;; ; If you wish to have an extension loaded automatically, use the following ; syntax: ; ; extension=modulename.extension ; ; For example, on Windows: ; ; extension=msql.dll ; ; ... or under UNIX: ; ; extension=msql.so ; ; ... or with a path: ; ; extension=/path/to/extension/msql.so ; ; If you only provide the name of the extension, PHP will look for it in its ; default extension directory. extension=pcntl.so Verify pcntl support again (support enabled this time): $ php -i | grep pcntl pcntl pcntl support => enabled Running your test script: $ php -f test.php In parent! In child!