how to use pcntl_fork() with Apache?

yegor256 picture yegor256 · Aug 31, 2012 · Viewed 8.3k times · Source

This is my code, inside index.php (just an example):

$pid = pcntl_fork();
if ($pid == -1) {
  die("failed to fork");
} else if ($pid) {
  // nothing to do
} else {
  putDataIntoWebService();
  exit();
}
echo "normal page content";

This snippet works fine in command line. In Apache exit() kills them both, the parent and the kid process. What is a workaround?

Answer

Hubro picture Hubro · Aug 31, 2012

You can't use the pcntl_* functions with the Apache module version of PHP. Quoting from a comment in the pcntl_fork documentation:

It is not possible to use the function 'pcntl_fork' when PHP is used as Apache module. 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()'