Saturday, 5 December 2015

Fatal error: Call to undefined function pcntl_fork() while running pcntl_fork as apache module

In the last post, we have seen that how to install pcntl modules in php and enable them. But if we are running php as an Apache module, you will get the following error :

Fatal error: Call to undefined function pcntl_fork()


Possible solutions given the PHP manual 

1. Workaround to pcntl_fork() not being usable when PHP is run as an Apache module

function background_job($program, $args)
{
    # The following doesn't work when running PHP as an apache module
    /*
    $pid = pcntl_fork();
    pcntl_signal(SIGCHLD, SIG_IGN);

    if ($pid == 0)
    {
        posix_setsid();
        pcntl_exec($program, $args, $_ENV);
        exit(0);
    }
    */

    # Workaround
    $args = join(' ', array_map('escapeshellarg', $args));
    exec("$program $args 2>/dev/null >&- /dev/null &");
}



2. Another solution given in the doc:

I was able to get around the problem of not being able to run fork and exec from Apache php.

I got around this by calling the system 'at' command on Linux.  "at run something now".  and you have to set atrun -s in a crontab file (to run every minute) to insure that things get kicked off quickly even if there is a heavy load on the machine.

If you're the only one running batch jobs on a linux box, this works.


No comments:

Post a Comment