0

I am currently working on a Symfony 6.1 project. I want to execute a Command as a Process from a Controller. However, I do not get it working. Everytime I execute the Command it fails with the Exit Code: (). The Command itself works like a charm if I run it via the cmd.

My current Code for executing the command as a Process:

        $cwd = substr(getcwd(), 0, strrpos(getcwd(), '/')).'/';
        $process = new Process(['php bin/console example:generate']); 
        $process->setWorkingDirectory($cwd);
        $process->start();

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

Other versions I have already tried out:

        $process = new Process(['php bin/console example:generate']);
        $process->start();

        $process = new Process(['php', 'bin/console', 'example:generate']); 
        $process->start();

        $process = new Process(['usr/bin/php', '/var/www/html/bin/console', 'example:generate']); 
        $process->start();

        $process = new Process(['usr/bin/php /var/www/html/bin/console example:generate']); 
        $process->start();

When I use the dd() Function to show the Process Object, I get the following Informations:

Symfony\Component\Process\Process {#2050
  -callback: null
  -hasCallback: false
  -commandline: array:1 [
    0 => "php bin/console example:generate"
  ]
  -cwd: "/var/www/html/"
  -env: []
  -input: null
  -starttime: null
  -lastOutputTime: null
  -timeout: 60.0
  -idleTimeout: null
  -exitcode: null
  -fallbackStatus: []
  -processInformation: null
  -outputDisabled: false
  -stdout: null
  -stderr: null
  -process: null
  -status: "ready"
  -incrementalOutputOffset: 0
  -incrementalErrorOutputOffset: 0
  -tty: false
  -pty: false
  -options: array:2 [
    "suppress_errors" => true
    "bypass_shell" => true
  ]
  -useFileHandles: false
  -processPipes: null
  -latestSignal: null
}

Can anyone tell me what I am doing wrong here?

schwaluck
  • 115
  • 9
  • Have you verified that the user profile the web server process runs as has `php` on its PATH variable and controller's working directory (`getcwd()`) is the one you think? – Álvaro González Oct 04 '22 at 08:08
  • @ÁlvaroGonzález Thanks for the feedback! I am logged in as root, but the webserver is running through the user "www:data". I typed "printenv" as root in the cmd and for the variable PATH it comes out as follows: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin Is something missing here? – schwaluck Oct 04 '22 at 08:57
  • For the working directory, I have so far assumed that this is the directory in which the "php bin/console example:generate" command should be executed. In my case this would be "/var/www/html/". However, with the getcwd() command I get "/var/www/html/public". Therefore I have adapted the cwd with subtr and strrpos accordingly. Do I have a wrong understanding of the working directory here? – schwaluck Oct 04 '22 at 08:57
  • Update: for the user "www:data" the "printenv" command returns the following: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/snap/bin – schwaluck Oct 04 '22 at 09:21
  • Regarding PATH, I don't know where your PHP interpreter is. You can find out with `which php`. Regarding working directory, it's where the entry script is. In Symfony that's `index.php`. – Álvaro González Oct 04 '22 at 11:31

0 Answers0