PHP Warning: exec() unable to fork

The problem is often caused by the system or the process or running out of available memory. Be sure that you have enough by running free -m. You will get a result like the following:

total used free shared buffers cached Mem: 7985 7722 262 19 189 803 -/+ buffers/cache: 6729 1255 Swap: 0 0 0

The buffers/cache line is what you want to look at. Notice free memory is 1255 MB on this machine. When running your program keep trying free -m and check free memory to see if this falls into the low hundreds. If it does you will need to find a way to run you program while consumer less memory.


I ran into same problem and I tried this and it worked for me;

ulimit -n 4096

Process limit

"Is there a process limit I should look into"

It's suspected somebody (system admin?) set limitation of max user process. Could you try this?

$ ulimit -a
....
....
max user processes              (-u) 16384
....

Run preceding command in PHP. Something like :

echo system("ulimit -a");

I searched whether php.ini or httpd.conf has this limit, but I couldn't find it.

Error Handling

"even a better way to handle these processes as to get around the error all together?"

The third parameter of exec() returns exit code of $cmd. 0 for success, non zero for error code. Refer to http://php.net/function.exec .

exec($cmd, &$output, &$ret_val);

if ($ret_val != 0)
{
    // do stuff here
}
else
{
    echo "success\n";
}

In my case (large PHPUnit test suite) it would say unable to fork once the process hit 57% memory usage. So, one more thing to watch for, it may not be a process limit but rather memory.

Tags:

Php

Centos