Tuesday, May 23, 2017

proc_open() fork fails in Laravel controller

I get an error saying: proc_open(): fork failed - Cannot allocate memory in a Laravel controller. The generate function is called with axios (Vuejs SAP).

This is what my controller looks like

use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use GifCreator\AnimGif;
use simpleimage\SimpleImage;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

class GifController extends Controller
{
    //
    public function generate(Request $request)
    {
        if(!$request->ajax()) return;

        $cmd = 'ls';
        $process = new Process($cmd);
        $process->run();

        // executes after the command finishes
        if (!$process->isSuccessful()) {
            throw new ProcessFailedException($process);
        }

        echo $process->getOutput();        

    }
}

free -m on server:

|       | total  | used  | free | shared | buff/cache | available |
| ----- | ------ | ----- | ---- | ------ | ---------- | --------- |
| Mem:  | 2000   | 243   | 577  | 20     | 1178       | 1541      |
| Swap: | 4095   | 0     | 4095 |        |            |           |

EDIT:

As Hilmi pointed out I've now tried adding a swap space, and now the command runs successfully sometimes – about half of the time.

Is there a way to retry the Process if it fails?



via kmaar

Advertisement