pcntl_fork() returning, Fatal error: Call to undefined function pcntl_fork()
Asked Answered
I

4

23

I'm trying to fork a command line run XAMPP php process using pcntl_fork(). When I run the command below:

$pid = pcntl_fork();
if($pid == -1){
    file_put_contents('testlog.log',"\r\nFork Test",FILE_APPEND);
    return 1; //error
}
else if($pid){
    return 0; //success
}
else{   
    file_put_contents($log, 'Running...', FILE_APPEND);
}

I get:

Fatal error: Call to undefined function pcntl_fork()

Can anyone suggest how to fix this?

Incidence answered 30/5, 2013 at 1:29 Comment(5)
What's your OS? Be advised, that Windows has no underlying *fork() syscalls.Grist
Have you successfully installed php5-pcntl ?Grist
No I didn't realize that was something that needed to be installed separate from XAMPP.Incidence
It is. If you installed PHP via MacPorts try port install php5-pcntlGrist
Sorry, I missed the XAMPP part. My bad.Grist
I
33

It is not possible to use the function 'pcntl_fork' when PHP is used as Apache module (such as XAMPP). You can only use pcntl_fork in CGI mode or from command-line.

Using this function will result in: 'Fatal error: Call to undefined function: pcntl_fork()'

Source: http://php.net/manual/en/function.pcntl-fork.php

Ingrown answered 9/12, 2013 at 14:13 Comment(1)
Adding to this, the OP should consider curl_multi_exec if running from a web page.Vorfeld
C
11

To see if it is installed, run:

php -i | grep pcntl

If it is present and enabled then the pcntl function are likely disabled, which appears to be the default in newer PHP 5.x installs. To check, run:

php -i | grep disable_functions

If you see a list of pcntl_* functions, you'll need to edit your php.ini file (inside of XAMPP) and comment out the line disable_functions=

I'd recommend you use this distribution of PHP for OS X, which has current versions and I can confirm does have the pcntl extension.

Coypu answered 11/11, 2013 at 15:51 Comment(2)
Commenting out "disabled_functions =" was a no go for me. #ubuntu1404 #php7.1Scrawny
disable_functions return warning about security reason; neither fatal error about undefined function.Immedicable
I
3

pcntl_* functions, Process Control support in PHP is not enabled by default. You have to compile the CGI or CLI version (don't used as Apache module) of PHP with --enable-pcntl configuration option when compiling PHP to enable Process Control support.

Currently, this module will not function on non-Unix platforms (Windows).

ref

Immedicable answered 6/6, 2018 at 3:41 Comment(0)
M
1

I had the same issue when running the script as a part of Apache. So, my solution was to put the code containing pcntl_fork in a different file (let's call it fork.php) and use exec() to run it, like this:

mainFile.php (this is what Apache will run)

exec('php fork.php',$output);

fork.php

$pid = pcntl_fork();
if($pid == -1){
    file_put_contents('testlog.log',"\r\nFork Test",FILE_APPEND);
    return 1; //error
}
else if($pid){
    return 0; //success
}
else{   
    file_put_contents($log, 'Running...', FILE_APPEND);
}
Meeting answered 19/3, 2020 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.