Home > Mobile >  Exec shell command in symfony "No such file or directory"?
Exec shell command in symfony "No such file or directory"?

Time:09-30

Using:

  • Laravel 8
  • PHP 8.0 (homebrew)

Problem: Running any shell commands using Symfony: Symfony\Component\Process\Process

The following code executes on php7.4, but not on php8.0, both within Laravel 8

        $newDir = storage_path('test');
        $process1 = new Process(["mkdir ". $newDir]);
        $process1->run();

        if (! $process1->isSuccessful()) {
            throw new ProcessFailedException($process1);
        }

The result on PHP 7.4 is that the test directory is created within Laravels storage directory.

The result on PHP 8.0 is that the exception ProcessFailedException is triggered, with the result.

Error Output:
================
sh: /Users/username/code/project-name/mkdir /Users/username/code/project-name/storage/test: No such file or directory
sh: line 0: exec: /Users/username/code/project-name/mkdir /Users/username/code/project-name/storage/test: cannot execute: No such file or directory

I have attempted to use other commands and all give the same result

cannot execute: No such file or directory

My suspicion is that it may be permissions related, but all related processes can be seen running as the user executing the process. What am i missing?

CodePudding user response:

Use a full path i.e. /bin/mkdir and separate the arguments instead of mkdir plus string concatenation.

$process1 = new Process(["/bin/mkdir", "-p", $newDir]);

The Process component can't find an executable with name mkdir [arguments] in your current working directory.

  • Related